【发布时间】:2019-07-14 00:58:29
【问题描述】:
我有一个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` is the output of the 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 模块?
【问题讨论】: