【发布时间】:2020-07-23 16:55:14
【问题描述】:
我有一些我喜欢在 nodejs (14.4) 和浏览器中使用的代码。为了让这段代码在 nodejs 中工作,我需要命名导入,例如
import {Vector3} from "three;
ES 6 模块在进行以下更改后总体上可以正常工作:
package.json:
"type": "module",
使用--experimental-specifier-resolution=node 启动nodejs,因此我不必指定文件扩展名。但是对于命名的导入,nodejs 仍然会打印出来:
SyntaxError: The requested module 'three' is expected to be of type CommonJS, which does not support named exports.
有一篇 Stackoverflow 帖子建议使用 esm 包加载器。不幸的是,它有一个错误使 TypeScript“反射元数据”无法使用(问题:https://github.com/standard-things/esm/issues/809)所以我不能使用它。
TL;DR;如何在没有 ESM 包加载器的情况下在 nodeJs 14.4 中启用命名的 ES 6 模块?类型:模块和启动参数已经设置。
【问题讨论】:
标签: node.js es6-modules