Nest: ERR_REQUIRE_ESM: Must Use import to load ES Module?

Created on 25 Apr 2020  路  10Comments  路  Source: nestjs/nest

Getting this issue when I run nest g module users any ideas?

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\USER\Documents\nest-project\node_modules\is-promise\index.js
require() of ES modules is not supported.
require() of C:\Users\USER\Documents\nest-project\node_modules\is-promise\index.js from C:\Users\USER\Documents\nest-project\node_modules\run-async\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\USER\Documents\nest-project\node_modules\is-promise\index.js to end in .cjs, change the requiring code to use import(), or remove "type":
"module" from C:\Users\USER\Documents\nest-project\node_modules\is-promise\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\Users\USER\Documents\nest-project\node_modules\run-async\index.js:3:17)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14) {
  code: 'ERR_REQUIRE_ESM'
}
needs triage question 馃檶

Most helpful comment

I am running into the same issue. Even when I run nest start --watch.

This issue also happens when I'm running any of the scripts, like npm run start or start:dev. I fixed it by going inside the is-promise package, and removed the "type: module" property-value inside package.json like it said in there. But I did not have this issue before 3 days ago, strange suddenly it's an issue?

I just checked my other nest project I had created 3 days ago and I did not see the "type: module" property-value in the npm package "is-promise"'s package.json file, so I don't know what caused it to add that property this time.

All 10 comments

I am running into the same issue. Even when I run nest start --watch.

I am running into the same issue. Even when I run nest start --watch.

This issue also happens when I'm running any of the scripts, like npm run start or start:dev. I fixed it by going inside the is-promise package, and removed the "type: module" property-value inside package.json like it said in there. But I did not have this issue before 3 days ago, strange suddenly it's an issue?

I just checked my other nest project I had created 3 days ago and I did not see the "type: module" property-value in the npm package "is-promise"'s package.json file, so I don't know what caused it to add that property this time.

Thanks @ansonfoong did that and it works for me too.

Problem only happens to me on node v12.16.2, but it does not happen on v10.20.1.

A new version of is-promise was released 2 hours ago. type: module was added in this commit.

If you're using Yarn, adding this to your package.json:

{
  "dependencies": {},
  "devDependencies": {},

  "resolutions": {
    "is-promise": "2.1.0"
  }
}

should fix the issue. I am not sure if there is a npm equivalent of "resolutions".

Oops, I think that new release of is-promise broke the CLI.

I created a new user on my machine and can't get the nest new command working.

internal/modules/cjs/loader.js:584
            if (e.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') throw e;
                                                            ^

Error [ERR_INVALID_PACKAGE_TARGET]: Invalid "exports" main target "index.js" defined in the package config /usr/local/lib/node_modules/@nestjs/cli/node_modules/is-promise/package.json
    at resolveExportsTarget (internal/modules/cjs/loader.js:542:13)
    at resolveExportsTarget (internal/modules/cjs/loader.js:581:20)
    at applyExports (internal/modules/cjs/loader.js:455:14)
    at resolveExports (internal/modules/cjs/loader.js:508:23)
    at Function.Module._findPath (internal/modules/cjs/loader.js:632:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1001:27)
    at Function.Module._load (internal/modules/cjs/loader.js:884:27)
    at Module.require (internal/modules/cjs/loader.js:1074:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/@nestjs/cli/node_modules/run-async/index.js:3:17) {
  code: 'ERR_INVALID_PACKAGE_TARGET'
}

This workaround fixed above issue on my machine:

diff --git a/package-lock.json b/package-lock.json
index 592f371..cb4cdb1 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -5293,9 +5293,9 @@
       }
     },
     "is-promise": {
-      "version": "2.2.0",
-      "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.0.tgz",
-      "integrity": "sha512-N/4ZxZGjDLAWJQNtcq1/5AOiWTAAhDwnjlaGPaC2+p3pQ+Ka2Dl/EL29DppuoiZ8Xr1/p/9ywBGGzHRPoWKfGA=="
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.1.0.tgz",
+      "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o="
     },
     "is-regex": {
       "version": "1.0.5",

Issue fixed by is-promise lib author commit:
https://github.com/then/is-promise/commit/68d3d6871806162772b33e9629908ded6c8bf0b1

Release 2.2.2

npm uninstall -g @nestjs/cli
npm install -g @nestjs/cli

Should fix the problem, it works for me now.

Confirmed to be working again. This issue can be closed.

Was this page helpful?
0 / 5 - 0 ratings