简体   繁体   中英

ESM changes are making conflict in my NestJS project

I'm the author of nestjs-cashify . I decided to update my package as well as the underlying packages. I'm encountering some issues after updating one the core packages that I'm using it. Since my package ts configuration is set to es6 not es2020 . (In other words, the new version of package that I'm using is changed to es2020 recently)

When I want to use my package in a regular NestJS project (in my example folder), I get these errors:

/home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js:21
const cashify_1 = require("cashify");
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/vahidnajafi/repos/nestjs-cashify/node_modules/cashify/dist/index.js from /home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js not supported.
Instead change the require of index.js in /home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js:21:19)
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/dist/index.js:18:14)
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/example/dist/app.module.js:12:26)
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/example/dist/main.js:4:22) {
  code: 'ERR_REQUIRE_ESM'
}

This makes sense since my project is set to "es6" and when I build, it uses "require" for ES module (core module is now ESM from new version): This is the error: require() of ES Module /index.js from /cashify.module.js not supported.

Then I decided to set my project to "es2020" as well. Now, this error is gone (since in my build result it's not using require anymore, it's using import statement).

In my example folder ( regular NestJS project ), which I'm testing my package, this config is set: "target": "es2017",

Now I see the following error:

SyntaxError: Unexpected token 'export'

IMPORTANT NOTE:

Even though that's not a good idea to change my example project's configuration ( since all NestJS developers will have to do this ), I tried to fix this anyway. I changed it to es2020 as well.

This time, I get the following errors:

(node:89687) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/home/vahidnajafi/repos/nestjs-cashify/example/dist/main.js:1
import { NestFactory } from '@nestjs/core';
^^^^^^

SyntaxError: Cannot use import statement outside a module

I also tried to set "type": "module" in package.json file (as mentioned in the error message) and then I see this error:

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/vahidnajafi/repos/nestjs-cashify/example/dist/app.module' imported from /home/vahidnajafi/repos/nestjs-cashify/example/dist/main.js

Any idea?

It looks like your modules are not resolving properly, probably because you are not using the new moduleResolution settings added in TS v4.7 .

You need to add the following to your TSConfig file.

    "module": "Node16", // Set your module Type to an ESM Module 
    "moduleResolution": "Node16", // Resolve your imports as an ESM Module
    "allowSyntheticDefaultImports": true, // Ease ESM support where the packages on the other-side need it.
Make sure you have added TS v4.7 using the following:

npm i -D typescript@latest

Once you have done the above, rebuild your entire project, then try running it. It should work, I have got the #74 error myself. The error stems from an improperly configured ESM-TS-Node project.

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