babel-preset-es2015
,并且与let a = 2;
下方的es6功能一样正常
但无法使用es6模块功能import fs from 'fs'
,如下所示:
$ babel-node --presets es2015
> let a = 2;
'use strict'
> a
2
> import fs from 'fs';
SyntaxError: repl: Modules aren't supported in the REPL
import fs from 'fs';
at File.buildCodeFrameError (/usr/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/index.js:407:15)
at NodePath.buildCodeFrameError (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/index.js:149:26)
at PluginPass.ModuleDeclaration (/usr/lib/node_modules/babel-cli/lib/_babel-node.js:78:20)
at newFn (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/visitors.js:262:19)
at NodePath._call (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:63:18)
at NodePath.call (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:47:17)
at NodePath.visit (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:93:12)
at TraversalContext.visitQueue (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:152:16)
at TraversalContext.visitMultiple (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:110:17)
at TraversalContext.visit (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:182:19)
那有什么不对? 谢谢!
答案 0 :(得分:8)
错误信息正如它所说的那样。您不能在REPL中使用ES6模块语法,它不受支持。您可以创建一个小型适配器,导入为ES6并导出为CommonJS:
# es6-to-common.js
import MyThing from './somewhere';
module.exports = MyThing;
现在在通常的babel-node
提示符中
> MyThing = require('./es6-to-common')
答案 1 :(得分:2)
从官方文档中获取:http://babeljs.io/docs/usage/cli/
ES6-style module-loading may not function as expected
Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.