使用Babel时出现意外保留字“import”

27

在我的NodeJSv4.1.1代码中使用Babel。

已经运行了require钩子:

require("babel-core/register");

$appRoot = __dirname;

module.exports = require("./lib/controllers/app");

在随后加载的.js文件中,我正在执行以下操作:

import { Strategy as LocalStrategy } from "passport-local";

但是这会在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)

Babel 的哪个版本? - max
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Felix Kling
1
使用es2015预设的@Rob,如果你将$passport = require('passport');更改为var $passport = require('passport');,你是否仍然会看到那个错误? - max
1
这是在运行纯Node时常见的错误,这意味着您可能会看到这个问题,因为Babel实际上并未运行。我建议按照文档所建议的切换到babel-register模块,因为您正在使用Babel 6。然后确保所涉及的文件没有被忽略。请参阅@pherris下面的评论。 - ian
@ian - 堆栈转储包括这一行:at loader (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5),这确实听起来像是在使用 babel-register... - Jules
显示剩余5条评论
3个回答

25

听起来你没有使用正确的预设。从babel6开始,核心babel加载器不再默认包含期望的ES6转换(它现在是一个通用代码转换平台),相反你必须使用一个预设:

require('babel-register')({
        "presets": ["es2015"]
});

您还需要安装预设包:

npm install --save-dev babel-preset-es2015

8
这段代码应该放在哪里?是在Webpack配置文件中吗?还是在Babel配置文件中?或者是在我正在运行的代码中? - user9903
1
@omouse 通常你会把这个放在你的主入口文件里,一般是在顶层的 index.js 文件中,在引入其余代码之前。 - Arkadiy Kukarkin
2
@Fractalf:babel-register 的转译器不适用于引用它的文件,因此您不能在同一文件中使用 import。您必须要求另一个文件,该文件可以使用完整的 es2015 语法。 - Arkadiy Kukarkin
1
@ArkadiyKukarkin:谢谢,所以我尝试只要求es6模块并仅在该模块中使用babel-core,但是然后出现了相同的错误,只是抱怨导出。我用babel-node将其全部工作,然后进行构建,但是对于第一次用户来说,这有多难!对于那些尚未接触过babel的人来说,文档非常糟糕:( - Fractalf
最终找到了一些能够使这个工作的东西:https://leanpub.com/setting-up-es6/read#sec_nodejs-babel-dynamic https://github.com/rauschma/node-babel-dynamic-demo - Fractalf
显示剩余5条评论

3

看起来这个文件没有被转译。随后加载的.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
});

默认情况下,所有对 node_modules 的 require 都将被忽略。你可以通过传递一个 ignore 正则表达式来覆盖这个设置。详见:https://babeljs.io/docs/usage/require/

我有后续的要求,这些要求需要其他东西,使用babel可以正常工作吗? - rcjsdev

0

当我尝试通过mocha运行测试时,遇到了问题,我通过将以下内容放入我的package.json文件中解决了它:

"babel": {
    "presets": [
      "es2015"
    ]
},

我不完全清楚这是如何工作的。我正在像这样运行测试:

mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive

最终,我想这一切都会有意义。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接