这是我的脚本test.js
:
import 'jsdom-global/register';
import 'canvas';
console.log('done');
这是我的 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"
}
}
当我运行时npx babel-node test.js
,我遇到了这个错误
/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)
这是我的.babelrc
档案
{
"presets": ["env", "stage-0"]
}
在我看来,babel-node
无法处理依赖 js 文件中的 es6 语法。我希望babel-node
能够即时加载和编译所有依赖项。我该如何解决这个问题?
版本信息:
$ node --version
v4.8.6
$ npm --version
2.15.11