【问题标题】:How can I check if an import statement is working correctly in the Node 6 REPL?如何检查导入语句在 Node 6 REPL 中是否正常工作?
【发布时间】:2016-10-09 19:06:49
【问题描述】:
export default config = {
  apiUrl : "http://localhost:7543"
}

$ node --version
v6.7.0

不转译这可能吗?有没有办法在不更改我的代码的情况下填充导入?

我想将其拉入控制台 (REPL) 并查看对象是否设置正确。但是:

import {config} from './config';
^^^^^^
SyntaxError: Unexpected token import

看起来我还不能在 REPL 中使用 import。那么有什么选择呢?我需要使用 ES5/Node require 吗?我可以为 Nesh 添加到我的加载器中吗?

理想情况下,我只想打开node 控制台并在使用import 的行中使用粘贴,而无需更改我的代码。

【问题讨论】:

标签: javascript node.js ecmascript-6 es6-modules


【解决方案1】:

Node.js 不支持 ES 模块。如果你想使用它们,你必须使用 Babel 转译你的代码。

无法对 ES 模块进行 polyfill,因为它们引入了新语法,因此代码甚至无法正确解析(您会遇到语法错误)。

【讨论】:

  • 这似乎是反对在 Node.js 中采用 ES2015 的论据。如果我不能将我的代码复制/粘贴到 REPL 中,那对我来说是不利的。
  • @jcollum 你可以使用babel-node
  • 否:SyntaxError: repl: Modules aren't supported in the REPL
猜你喜欢
  • 2016-03-04
  • 2013-08-10
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 2014-01-11
  • 2020-04-23
相关资源
最近更新 更多