使用babel时意外保留字'import' [英] Unexpected reserved word 'import' when using babel
本文介绍了使用babel时意外保留字'import'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取需要的钩子:
需要( 巴别核/注册);
$ appRoot = __dirname;
module.exports = require(./ lib / controllers / app);
在随后的 .js
文件中我在做:
import策略为LocalStrategy从护照本地;
然而,这是在CLI中生成以下错误:
$ b来自护照本地的$ b
import {策略为LocalStrategy};
^^^^^
语法错误:意外的保留字
在exports.runInThisContext(vm.js:53:16)
在Module._compile(模块.js:413:25)
在加载器(/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5)
在Object.require.extensions。(匿名函数)[as .js](/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:138:7 )
在Module.load(module.js:355:32)
在Function.Module._load(module.js:310:12)
在Module.require(module.js: 365:17)
at require(module.js:384:17)
在module.exports(index.js:9:5)
在Object。< anonymous> (app.js:102:39)
解决方案
你没有使用正确的预设。根据babel 6,核心的babel加载器默认情况下不再包含预期的ES6转换(现在是一个通用代码转换器平台),而是必须使用预设:
require('babel-register')({
presets:[es2015]
});
您还需要安装预设包:
npm install --save-dev babel-preset-es2015
Using Babel in my NodeJSv4.1.1 code.
Got the require hook in:
require("babel-core/register");
$appRoot = __dirname;
module.exports = require("./lib/controllers/app");
In a subsequently lodaded .js
file I am doing:
import { Strategy as LocalStrategy } from "passport-local";
However this is generating the following error in the 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)
解决方案
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:
require('babel-register')({
"presets": ["es2015"]
});
You will also need to install the preset package:
npm install --save-dev babel-preset-es2015
这篇关于使用babel时意外保留字'import'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文