简体   繁体   English

在 Nest.js 中对 ESM 模块使用导入会给出 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()

[英]Using import for an ESM Module in nest.js gives [ERR_REQUIRE_ESM]: require() of ES Module not supported

I use nest.js with typescript and wanted to add我将nest.js与typescript一起使用并想添加

import { DRACOLoader, GLTFLoader, TextureLoader } from 'node-three-gltf'; 

in one of my modules.在我的一个模块中。 However that results in below error但是,这会导致以下错误

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)

And node-three-gltf@1.0.3 which I use is just an esm module.而我使用的 node-three-gltf@1.0.3 只是一个 esm 模块。 Resulting in the (at least to me - fairly new to this suff) weird situation of me using ESM import syntax in my typescript module/controller to import the ESM module node-three-gltf and getting this error.导致我在我的 typescript 模块/控制器中使用 ESM 导入语法来导入 ESM 模块 node-three-gltf 并得到这个错误的奇怪情况(至少对我来说 - 对这个来说是相当新的)。

Seems to be due to the fact that nest.js build of my project transforms my ES syntax to CJS syntax and thus replaces my import with require but does not transform the node-three-gltf module and then complains.似乎是由于我的项目的 nest.js 构建将我的 ES 语法转换为 CJS 语法,因此用 require 替换了我的导入,但没有转换 node-three-gltf 模块然后抱怨。

my tsconfig goes like this:我的 tsconfig 是这样的:

{
   "compilerOptions": {
      "module": "commonjs",
      "moduleResolution": "Node",
      "target": "esnext",
...

Theoretically I see the following options:从理论上讲,我看到以下选项:

  • ensure i build everything as ESM ( I tried that via setting module in my tsconfig to ES2020 and adding "type":"module" to my package.json which then lead to a new import error for a different dependency (确保我将所有内容构建为 ESM(我尝试通过将 tsconfig 中的模块设置为 ES2020 并将“type”:“module”添加到我的 package.json 中,然后导致不同依赖项的新导入错误(

node_modules\connect-typeorm\out' is not supported resolving ES modules imported from C:\m3\dist\src\main.js Did you mean to import connect-typeorm/out/index.js?不支持 node_modules\connect-typeorm\out' 解析从 C:\m3\dist\src\main.js 导入的 ES 模块您的意思是导入 connect-typeorm/out/index.js 吗?

  • make sure the node-three-gltf provides a cjs version as build, for which it appears I could raise a PR but would need to undestand the build tools fo that dependency, so not really good option确保 node-three-gltf 提供了一个 cjs 版本作为构建,看起来我可以提出一个 PR 但需要了解该依赖项的构建工具,所以不是很好的选择
  • upgrade to node-three-gltf@1.10.0 as that has a cjs export but which however requires Node 18 which I cannot use in production at this time for reasons out of my control升级到 node-three-gltf@1.10.0,因为它有一个 cjs 导出,但是它需要 Node 18,由于我无法控制的原因,我目前无法在生产中使用它
  • adapt the nest.js build in way that it does transform esm dependencies to cjs - which I don't know how to do.以将 esm 依赖项转换为 cjs 的方式调整 nest.js 构建 - 我不知道该怎么做。

So I wonder if sb can advise me on how to adjust the nest.js build config to do the esm->cjs transformation for dependencies or point me in another direction?所以我想知道 sb 是否可以建议我如何调整 nest.js 构建配置以对依赖项进行 esm->cjs 转换或将我指向另一个方向?

Thanks!谢谢! T

I believe you should stay with CJS in your app, and use the import() expression to load that ESM-only package.我相信您应该在您的应用程序中使用 CJS,并使用import()表达式来加载仅限 ESM 的 package。

See: Compile a package that depends on ESM only library into a CommonJS package请参阅: 将仅依赖于 ESM 的库的 package 编译为 CommonJS package

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 [ERR_REQUIRE_ESM]: ES 模块的 require()... 不支持 - Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - 但我用的是 import! - webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - But I AM using import! 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Vue 3 Typescript - Error [ERR_REQUIRE_ESM]: require() of ES Module - Vue 3 Typescript 错误 [ERR_REQUIRE_ESM]: 运行脚本时必须使用 import 加载 ES 模块 - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module when running script 错误 [ERR_REQUIRE_ESM]:运行 `prisma db seed` 时 ES 模块的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module when running `prisma db seed ` Nanoid4 代码错误 [ERR_REQUIRE_ESM]: ES 模块的 require() - Nanoid4 in codecept error [ERR_REQUIRE_ESM]: require() of ES Module 在 ERR_REQUIRE_ESM 中使用 lowdb 结果 - Using lowdb results in ERR_REQUIRE_ESM 如何在打字稿项目中导入节点模块。 ERR_REQUIRE_ESM - How to import node module in typescript project. ERR_REQUIRE_ESM TS ERR_REQUIRE_ESM -&gt; 不能在模块外使用 import 语句 -&gt; ERR_UNKNOWN_FILE_EXTENSION -&gt; 重复 - TS ERR_REQUIRE_ESM -> Cannot use import statement outside a module -> ERR_UNKNOWN_FILE_EXTENSION -> repeat 如何在节点和打字稿项目中导入 d3.js? [ERR_REQUIRE_ESM] - How to import d3.js in a node and typescript project? [ERR_REQUIRE_ESM]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM