我使用nest.js和类型记录,并想要添加
import { DRACOLoader, GLTFLoader, TextureLoader } from 'node-three-gltf';
在我的一个模块里。但是,这会导致以下错误
c:\m3\dist\src\gltftest\gltftest.controller.js:23
const node_three_gltf_1 = require("node-three-gltf");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module c:\m3\node_modules\node-three-gltf\build\index.js from c:\m3\dist\src\gltftest\gltftest.controller.js not supported.Instead change the require of index.js in c:\m3\dist\src\gltftest\gltftest.controller.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (c:\m3\dist\src\gltftest\gltftest.controller.js:23:27)
at Object.<anonymous> (c:\m3\dist\src\gltftest\gltftest.module.js:12:30)
我使用的节点-3-gltf@1.0.3只是一个esm模块。这导致了(至少对我来说)这种奇怪的情况--在我的类型记录模块/控制器中使用ESM导入语法来导入ESM模块节点--3-gltf并得到这个错误。
这似乎是因为我的项目的nest.js构建将我的ES语法转换为CJS语法,从而用require替换了我的导入,但是没有转换节点--三个gltf模块,然后再进行抱怨。
我的tsconfig如下所示:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "Node",
"target": "esnext",
...
从理论上讲,我看到了以下几种选择:
)。
不支持解析从
导入的ES模块,您是指导入connect/out/index.js吗?
确保节点--3-gltf为构建提供了一个cjs版本,对于该版本,我似乎可以引发一个PR,但需要解除针对该依赖项的构建工具,所以对于节点--3-gltf@1.10.0来说并不是很好的option
。
因此,我想知道sb是否可以建议我如何调整nest.js构建配置,以便为依赖项执行esm->cjs转换,还是指向另一个方向?
谢谢!T
发布于 2022-08-19 17:36:06
我认为您应该在您的应用程序中使用CJS,并使用import()
表达式加载该ESM专用包。
请参阅:Compile a package that depends on ESM only library into a CommonJS package
https://stackoverflow.com/questions/73419905
复制相似问题