简体   繁体   中英

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

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. 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.

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.

my tsconfig goes like this:

{
   "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 (

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?

  • 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
  • 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
  • adapt the nest.js build in way that it does transform esm dependencies to cjs - which I don't know how to do.

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?

Thanks! T

I believe you should stay with CJS in your app, and use the import() expression to load that ESM-only package.

See: Compile a package that depends on ESM only library into a CommonJS package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM