Javascript 使用 babel 时出现意外的保留字“import”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33710319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpected reserved word 'import' when using babel
提问by absolutevibe
Using Babel in my NodeJSv4.1.1 code.
在我的 NodeJSv4.1.1 代码中使用 Babel。
Got the require hook in:
得到了 require 钩子:
require("babel-core/register");
$appRoot = __dirname;
module.exports = require("./lib/controllers/app");
In a subsequently lodaded .js
file I am doing:
在随后加载的.js
文件中,我正在做:
import { Strategy as LocalStrategy } from "passport-local";
However this is generating the following error in the CLI:
但是,这会在 CLI 中生成以下错误:
import { Strategy as LocalStrategy } from "passport-local";
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at loader (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at module.exports (index.js:9:5)
at Object.<anonymous> (app.js:102:39)
采纳答案by Arkadiy Kukarkin
Sounds like you aren't using the right presets. As of babel 6, the core babel loader no longer includes the expected ES6 transforms by default (it's now a generic code transformer platform), instead you must use a preset:
听起来您没有使用正确的预设。从 babel 6 开始,核心 babel 加载器默认不再包含预期的 ES6 转换(它现在是一个通用的代码转换器平台),而是您必须使用预设:
require('babel-register')({
"presets": ["es2015"]
});
You will also need to install the preset package:
您还需要安装预设包:
npm install --save-dev babel-preset-es2015
回答by pherris
It seems that this file is not being transpiled. Is this subsequently loaded .js
file in the node_modules directory? If so, you need to:
这个文件似乎没有被转译。这是随后.js
在 node_modules 目录中加载的文件吗?如果是这样,您需要:
require("babel-core/register")({
// This will override `node_modules` ignoring - you can alternatively pass
// an array of strings to be explicitly matched or a regex / glob
ignore: false
});
By default all requires to node_modules will be ignored. You can override this by passing an ignore regex
默认情况下,所有对 node_modules 的要求都将被忽略。您可以通过传递忽略正则表达式来覆盖它
回答by Cameron O'Rourke
I was hitting the problem when trying to run tests via mocha, and I solved it by putting this in my package.json file:
我在尝试通过 mocha 运行测试时遇到了这个问题,我把它放在我的 package.json 文件中解决了这个问题:
"babel": {
"presets": [
"es2015"
]
},
I'm not completely clear on how this works. I'm running tests like this:
我并不完全清楚这是如何工作的。我正在运行这样的测试:
mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive
Eventually, this will all make sense I suppose.
最终,我想这一切都是有意义的。