[英]ES6 style imports in Node.js REPL
Coming from a Python background and being new to Node.js, I've found it surprisingly difficult to interactively try code with Node.js REPL. 来自Python背景并且是Node.js的新手,我发现使用Node.js REPL交互式尝试代码非常困难。 One key problem that I have faced is with imports
: 我遇到的一个关键问题是imports
:
ES6 style imports won't work on the Node.js REPL and I have to use CommonJS style imports using require
. ES6样式导入不能在Node.js REPL上运行,我必须使用require
使用CommonJS样式导入。 Example: I can't write import rxjs
at the Node prompt and have to use require('rxjs')
. 示例:我无法在Node提示符下编写import rxjs
并且必须使用require('rxjs')
。 This makes it harder to copy paste scripts into Node REPL to quickly test them and I have to first convert all ES6 style imports to require imports which feels counterintuitive. 这使得将粘贴脚本复制到Node REPL中以便快速测试它们变得更加困难,我必须首先将所有ES6样式的导入转换为需要导入的,这种导入感觉违反直觉。
Is there any simple way to be able to use ES6 style imports from the Node.js REPL ? 有没有简单的方法可以使用Node.js REPL中的ES6样式导入?
Like: 喜欢:
$ node $ node
> import 'rxjs'; > import'rxjs';
> import {map} from 'rxjs/operator/map'; >从'rxjs / operator / map'导入{map};
I even tried babel-node
which doesn't seem to support module imports. 我甚至尝试过babel-node
,它似乎不支持模块导入。
I'm afraid that the answer is no , or at least not yet . 我担心答案是否定的 ,或者至少还没有 。 We know that it will be supported in the future, but the ES2015 import
and export
won't act as in-place substitutions to CommonJS require
and module.exports
. 我们知道它将来会得到支持,但ES2015的import
和export
不会作为CommonJS require
和module.exports
的就地替换。 This is simply because they don't work the same way. 这只是因为它们的工作方式不同。 You may find more information in this Node.js blog post and this SO question . 您可以在此Node.js博客文章和此SO问题中找到更多信息。 In particular, one can already run and import standard modules in Node.js, under the flag --experimental-modules
(relevant 2ality blog post ). 特别是,人们已经可以在标志--experimental-modules
(相关的2ality博客文章 )下运行和导入Node.js中的标准模块。 However, this feature will still not work from the REPL. 但是,REPL仍然无法使用此功能。
Your idea of using babel-node
(which is now called babel-cli
) was not that bad: babel
implements an interoperable require
function, which is kind of a shim that can retrieve modules designed for either type system. 你使用babel-node
(现在称为babel-cli
)的想法并不是那么糟糕: babel
实现了一个可互操作的require
函数,它是一种可以检索为这两种类型系统设计的模块的垫片。 Unfortunately, the official website claims lack of support for this functionality in the REPL: 不幸的是, 官方网站声称在REPL中缺少对此功能的支持:
ES6-style module-loading may not function as expected ES6样式的模块加载可能无法按预期运行
Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL. 由于技术限制,在babel节点REPL中不完全支持ES6样式的模块加载。
Nonetheless, this issue does not apply to production code, which should not rely on the REPL anyway. 尽管如此,这个问题并不适用于生产代码,而生产代码无论如何都不应该依赖于REPL。 Instead of using the REPL, you may consider writing a script in a bare-bones project containing babel-cli
, and create a script for running the transpiled version: 您可以考虑在包含babel-cli
项目中编写脚本,并创建用于运行转换版本的脚本,而不是使用REPL:
{
"name": "my-test-chamber",
"private": true,
"scripts": {
"build": "babel src/main.js -o lib/main.js",
"start": "npm run build && node lib/main.js"
},
"dev-dependencies": {
"babel-cli": "^6.0.0"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.