Here is my script test.js
:
import 'jsdom-global/register';
import 'canvas';
console.log('done');
And here is my package.json:
{
"name": "test-jsdom",
"description": "Test",
"version": "0.1.0",
"author": "anthony@me.com",
"dependencies": {
"canvas": "^1.6.7"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-0": "^6.24.1",
"jsdom": "^11.3.0",
"jsdom-global": "^3.0.2"
}
}
When I run npx babel-node test.js
, I encountered this error
/Users/antkong/test/node_modules/jsdom/lib/api.js:10
const { URL } = require("whatwg-url");
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Module._extensions..js (module.js:416:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/antkong/test/node_modules/babel-register/lib/node.js:152: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 globalJsdom (/Users/antkong/test/node_modules/jsdom-global/index.js:28:15)
at Object.<anonymous> (/Users/antkong/test/node_modules/jsdom-global/register.js:1:19)
And it is my .babelrc
file
{
"presets": ["env", "stage-0"]
}
It seems to me babel-node
cannot handle es6 syntax in the dependent js file. I expect babel-node
to be able to load and compile all dependency on the fly. How can I fix this problem?
Version info:
$ node --version
v4.8.6
$ npm --version
2.15.11
Should use more up to date version of node.
I use nvm
to switch node to version 8.7.0 and the issue is fixed.