cashify icon indicating copy to clipboard operation
cashify copied to clipboard

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

Open vahidvdn opened this issue 2 years ago • 4 comments

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 avatar Jun 05 '22 11:06 vahidvdn

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 avatar Jun 05 '22 12:06 vahidvdn

@xxczaki Any idea on this?

vahidvdn avatar Jun 14 '22 09:06 vahidvdn

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.

talkquazi avatar Jun 18 '22 14:06 talkquazi

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

d2xdt2 avatar Sep 15 '22 14:09 d2xdt2

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

vahidvdn avatar Jun 20 '23 14:06 vahidvdn