n-riesco / jp-babel

jp-babel is a babel kernel for the Jupyter notebook
Other
86 stars 16 forks source link

kernel errors on some es6 features in required submodules #12

Closed cloudonshore closed 7 years ago

cloudonshore commented 7 years ago

Hello! Thanks for your work thus far on this. The error I'm encountering happens when requiring an es6 sub module inside a jp-babel notebook session.

For example, if I have a file called my-module.js which contains a destructured assignment:

const {a} = {a: 'val'};
module.exports = a;

and I require it in the notebook with this statement: const m = require('my-module');

it will generate the following error:

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at loader (/usr/local/lib/node_modules/jp-babel/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/jp-babel/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at evalmachine.<anonymous>:3:17
    at Object.exports.runInThisContext (vm.js:54:17)

however, if I change my-module.js to:

const b = {a: 'val'}, a = b.a;
module.exports = a;

I can require it successfully in the notebook. For some reason the const variable declaration is fine, but the destructered assignment isn't. However, destructured assignment works fine if I do it within the notebook.

I'm confused as to why all es6 features will work in the notebook, but the only es6 feature that seems to work in a submodule required within the notebook is const. If the required submodules weren't being babel compiled I would expect the supported es6 features to be all or nothing.

Also, default variables (such as function test(a='value'){}) work within the notebook, but throws this error when they're included in a submodule:

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at loader (/usr/local/lib/node_modules/jp-babel/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/jp-babel/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at evalmachine.<anonymous>:3:17
    at Object.exports.runInThisContext (vm.js:54:17)

hopefully this is easily reproducible. Once again, thank you for your work on this thus far, it's been really useful for me in my work.

cloudonshore commented 7 years ago

I figured out the answer to my question, require('babel-register') needs to be run inside the notebook before es6 modules can be required.