【问题标题】:babel-node es6 "Modules aren't supported in the REPL"babel-node es6 "REPL 不支持模块"
【发布时间】:2016-03-24 09:38:08
【问题描述】:

babel-preset-es2015 已安装,并且可以使用 es6 功能,如下面let a = 2;
但无法使用 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)

那怎么了?
谢谢!

【问题讨论】:

  • 错误信息是不言自明的......事实上,命令行REPL界面不支持模块。
  • @JoeClay 如果在没有--presets es2015的情况下运行babel-node,也会在eval let a = 2;时提示xx aren't supported in the REPL,当添加--presets es2015时,let a = 2;就可以了。所以我想知道我是不是忘了做点什么?
  • @JoeClay 让我震惊的是我不知道“REPL”是什么,所以我不同意它是不言自明的。看到您的评论后搜索定义对我有帮助,但可能更清楚。
  • @Dezza:是的,你说得有道理,回想起来我的评论有点居高临下。道歉!

标签: ecmascript-6 babeljs


【解决方案1】:

错误消息正是它所说的。您不能在 REPL 中使用 ES6 模块语法,它不受支持。您可以创建一个小型适配器,以 ES6 形式导入并以 CommonJS 形式导出:

# es6-to-common.js
import MyThing from './somewhere';
module.exports = MyThing;

现在在您通常的babel-node 提示符中:

> MyThing = require('./es6-to-common')

【讨论】:

    【解决方案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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多