nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

ERR_REQUIRE_ESM: Must Use import to load ES Module? #4677

Closed stuyy closed 4 years ago

stuyy commented 4 years ago

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'
}
anohabbah commented 4 years ago

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

stuyy commented 4 years ago

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.

anohabbah commented 4 years ago

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

mknaf commented 4 years ago

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

msheakoski commented 4 years ago

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

msheakoski commented 4 years ago

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

alejandrocoding commented 4 years ago

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'
}
martinburger commented 4 years ago

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",
alejandrocoding commented 4 years ago

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.

msheakoski commented 4 years ago

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

nhhockeyplayer commented 2 years ago

I did everything in this ticket to no avail;

cannot load nest models anymore

> nx run api-typegoose:serve 
chunk (runtime: main) main.js (main) 45.5 KiB (javascript) 937 bytes (runtime) [entry] [rendered]
webpack compiled successfully (19b9838b05893a4a)
Debugger listening on ws://localhost:9229/5c219b58-7461-47ca-8f83-810bf260b673
Debugger listening on ws://localhost:9229/5c219b58-7461-47ca-8f83-810bf260b673
For help, see: https://nodejs.org/en/docs/inspector
Issues checking in progress...

file:///Users/meanstack02/new/starter/dist/apps/api-typegoose/webpack:/starter/external commonjs "@nestjs/core":1
module.exports = require("@nestjs/core");
^
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/meanstack02/new/starter/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at Object.@nestjs/core (file:///Users/meanstack02/new/starter/dist/apps/api-typegoose/webpack:/starter/external commonjs "@nestjs/core":1:1)
    at __webpack_require__ (file:///Users/meanstack02/new/starter/dist/apps/api-typegoose/webpack:/starter/webpack/bootstrap:19:1)
    at file:///Users/meanstack02/new/starter/dist/apps/api-typegoose/main.js:1702:70
    at file:///Users/meanstack02/new/starter/dist/apps/api-typegoose/main.js:1777:3
    at file:///Users/meanstack02/new/starter/dist/apps/api-typegoose/main.js:1782:12
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at loadESM (node:internal/process/esm_loader:88:5)
    at handleMainPromise (node:internal/modules/run_main:65:12)
No issues found

the ecosystem has imposed this now on all

can we get some assist?

nhhockeyplayer commented 2 years ago

can nestjs hold pace with the higher level frameworks which are STANDARD place now

Im an angular monorepo developer

the support for nestJS backends is huge now

and im trying to run with nestJS in an elite fashion with the new build scaffolds

high velocity

currently shutdown

kamilmysliwiec commented 2 years ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.