I'm having difficulty getting org-babel to evaluate this block of code with
>emacs v24.5<br/>
node v5.0.0<br/>
babel-node v.6.6.5
<pre><code> #+BEGIN_SRC js :cmd "babel-node"
let arr = [1, 2];
let [x, y] = arr;
console.log(x);
console.log(y);
#+END_SRC
</pre>
<br />
The output looks like this
<pre><code>/tmp/babel-3700Vaq/js-script-37003RN:2<br />
let arr = [1, 2];
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at loader (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:126:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at /usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:161:27
at Object.<anonymous> (/usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:162:7)
at Module._compile (module.js:425:26)*
</pre>
But this source block seems fine
<pre><code>
#+BEGIN_SRC js :cmd "babel-node"
const numbers = [10,20,30,50];
const multiplyBy10 = numbers.map(a => a * 10);
console.log(multiplyBy10);
#+END_SRC
#+RESULTS:
| 100 | 200 | 300 | 500 |
</pre>
<br/>
>Edit:
Added self executing function with "use strict"
<pre><code>
#+BEGIN_SRC js :cmd "babel-node"
(function xy() {
"use strict";
let arr = [1, 2];
let [x, y] = arr;
console.log(x);
console.log(y);
})()
#+END_SRC
#+RESULTS:
</pre>
The output looks like this
<pre><code>
/tmp/babel-13529OHt/js-script-13529MVq:6
let [x, y] = arr;
^
SyntaxError: Unexpected token [
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at loader (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:126:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at /usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:161:27
at Object.<anonymous> (/usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:162:7)
at Module._compile (module.js:425:26)
</pre>
<br />
> Edit: Thanks to @ebpa i've managed to solve it with
<pre><code>npm install -g babel-preset-es2015</pre>
<br />
This is the output i'm getting after evaluating the default source block, which doesn't have a self executing function wrapper.
<pre><code>
/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:372
throw new Error("Couldn't find preset " + JSON.stringify(val) + " relative to directory " + JSON.stringify(dirname));
^
Error: Couldn't find preset "es2015" relative to directory "/tmp/babel-270346ez"
at /usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:372:17
at Array.map (native)
at OptionManager.resolvePresets (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:364:20)
at OptionManager.mergePresets (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:348:10)
at OptionManager.mergeOptions (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:307:14)
at OptionManager.init (/usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:465:10)
at compile (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:81:45)
at loader (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:126:14)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
</pre>
<br />
>Edit: Fire up a terminal and ran babel-node
<br />
<pre><code>
> require('babel-preset-es2015');
</pre>
Output
<pre><code>
Error: Cannot find module 'babel-preset-es2015'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at repl:1:1
at Object.exports.runInThisContext (vm.js:54:17)
at _eval (/home/johnwind/.nvm/versions/node/v5.0.0/lib/node_modules/babel-cli/lib/_babel-node.js:102:26)
at REPLServer.replEval (/home/johnwind/.nvm/versions/node/v5.0.0/lib/node_modules/babel-cli/lib/_babel-node.js:187:14)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
</pre>
> Edit: A follow up to getting it up and running
>http://rwx.io/posts/org-with-babel-node-updated/
<br \>
> Edit: This post gave me some clue to this piece of to puzzle
> https://phabricator.babeljs.io/T6723
> http://discuss.babeljs.io/t/error-parsing-jsx-with-global-installation-babel-preset-react/59/6
<br \>
> <h2>Edit: Finally got it working by installing a local copy</h2>
<pre><code>
$ mkdir local_babel
$ cd local_babel
$ npm init
$ npm install --save-dev babel-cli
$ npm install --save-dev babel-core
$ npm install --save-dev babel-preset-es2015
$ npm install --save-dev babel-preset-stage-0
$ npm install --save-dev babel-preset-stage-1
$ npm install --save-dev babel-preset-stage-2
$ npm install --save-dev babel-preset-stage-3
</pre>
created a softlink for babel-node
<pre><code>
$ cd /usr/local/bin
$ ln -s ~/local_babel/node_modules/babel-cli/bin/babel-node.js org-babel-node
</pre>
.zshrc
<pre><code>
export npm_config_prefix=$HOME/.node_modules
export PATH=$PATH:$HOME/.node_modules/bin
</pre>
<br \>
Added this to my init.el
<pre><code>
(setenv "NODE_PATH"
(concat
"/home/johnwind/local_babel/node_modules" ":"
(getenv "NODE_PATH")
)
)
</pre>
<br \>
output
<pre><code>
#+BEGIN_SRC js :cmd "org-babel-node --presets es2015"
let arr = [1, 2];
let [x, y] = arr;
console.log(x);
console.log(y);
#+END_SRC
#+RESULTS:
: 1
: 2
: undefined
</pre>
<br \>
For now I have both local and global copy of babel-cli, babel-core, babel-preset-es2015 . Which still couldn't quite wrap my head around this as it's kind of a waste of resources.
But it works.