如何在Node.js REPL中导入ES模块?

Min-Soo Pipefeet:

我有一个ES6模块right.mjs将其作为参数node很好运行:

$ node --version
v8.10.0
$ node --experimental-modules right.mjs
(node:4492) ExperimentalWarning: The ESM module loader is experimental.
executing right module

executing right module 是模块的输出。

与此相反,REPL中的以下输入等待进一步的输入:

$ node --experimental-modules
> (node:4526) ExperimentalWarning: The ESM module loader is experimental.

> import 'right.mjs';
... 

我不明白为什么。

与:

> import './right.mjs';
... 

尝试require导致:

> require('./right.mjs');
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/xxx/right.mjs
    at Object.Module._extensions..mjs (module.js:686:11)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

那么,如何在Node.js REPL中导入ES模块?

可能是Flask:

目前这是不可能的。ES模块应该从ES模块范围导入,而REPL不被认为是其中之一。随着时间的推移,这会有所改善,因为ES模块的支持是实验性的。在Node模块实现require的使用import是互斥的,REPL已使用require

importREPL中期望对动态的支持但尚不支持,最新的Node 11版本会导致错误:

TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]:未指定动态导入回调。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章