Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM changes in v3.0.0+ have some conflicts with my app #23

Closed
vahidvdn opened this issue Jun 5, 2022 · 5 comments
Closed

ESM changes in v3.0.0+ have some conflicts with my app #23

vahidvdn opened this issue Jun 5, 2022 · 5 comments

Comments

@vahidvdn
Copy link
Contributor

vahidvdn commented Jun 5, 2022

I'm the author of nestjs-cashify. I decided to update my package as well. I'm encountering some issues after updating to Cashify 3+ and building my package. Since my package ts configuration is set to es6 not es2020.

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 (Cashify module is now ESM from version 3): 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), this config is set: "target": "es2017",

Now I see the following error:

SyntaxError: Unexpected token 'export'

Even though that's not a good idea to change my example project's configuration (since all NestJS developers will have this conflict), 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 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?

@vahidvdn
Copy link
Contributor Author

vahidvdn commented Jun 5, 2022

The bottom line is a NestJS developer can not add this package to his project without changing the default configurations. Since the majority of the community is still using es2017, maybe it's not the best idea to use es2020.

@vahidvdn
Copy link
Contributor Author

@xxczaki Any idea on this?

@talkquazi
Copy link

Same here, There is another thread about the breaking changes here but the developer doesn't seem to care that he broke his package for over half of the module users who use NodeJS.

@d2xdt2
Copy link

d2xdt2 commented Sep 15, 2022

@vahidvdn I am maintaining an ES2017 fork of this package at https://www.npmjs.com/package/cashify-es2017 , which means it can be used as any other package.

@vahidvdn
Copy link
Contributor Author

Upgrading to the latest version of nestjs (v10) fixed the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants