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

Wrong error annotation when commonjs requires an ES module #55350

Open
targos opened this issue Oct 10, 2024 · 7 comments
Open

Wrong error annotation when commonjs requires an ES module #55350

targos opened this issue Oct 10, 2024 · 7 comments
Labels
confirmed-bug Issues with confirmed bugs.

Comments

@targos
Copy link
Member

targos commented Oct 10, 2024

Version

  • ^22.4.0
  • main with the --no-experimental-require-module flag.

Platform

macOS arm64

Subsystem

esm,module

What steps will reproduce the bug?

mkdir undefined && cd undefined
echo '{"type":"module"}' > package.json
echo "import nothing from 'somewhere'" > app.js
echo "require('./app.js')" > test.cjs
node test.cjs

How often does it reproduce? Is there a required condition?

It starts to happen with v22.4.0. v20.x and <=22.3.0 are not affected.

What is the expected behavior? Why is that the expected behavior?

$ node test.cjs
/Users/mzasso/git/test/undefined/test.cjs:1
require('./app.js')
^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mzasso/git/test/undefined/app.js from /Users/mzasso/git/test/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/mzasso/git/test/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/mzasso/git/test/undefined/test.cjs:1:1) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.3.0

What do you see instead?

$ node test.cjs
/Users/mzasso/git/test/undefined/test.cjs:315
undefined
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mzasso/git/test/undefined/app.js from /Users/mzasso/git/test/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/mzasso/git/test/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at Object.<anonymous> (/Users/mzasso/git/test/undefined/test.cjs:1:1) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.4.0

Additional information

As you can see, this seems to be caused by the presence of TracingChannel.traceSync in the stack trace.

@avivkeller avivkeller added the confirmed-bug Issues with confirmed bugs. label Oct 10, 2024
@targos
Copy link
Member Author

targos commented Oct 10, 2024

Probably caused by #44340 @Qard

@Qard
Copy link
Member

Qard commented Oct 10, 2024

@RafaelGSS is the one that finished and landed that PR, so they would probably know better what is going on here.

@RafaelGSS
Copy link
Member

Sorry, I've just seen it. I'll check when I get some time (currently traveling for 2 weeks)

@geeksilva97
Copy link
Contributor

geeksilva97 commented Oct 27, 2024

Would anybody guide me to where this part of the message,

/Users/mzasso/git/test/undefined/test.cjs:1
require('./app.js')
^

, is generated?

I thought it was

// Print (filename):(line number): (message).
but I'm wrong

@geeksilva97
Copy link
Contributor

Would anybody guide me to where this part of the message,

/Users/mzasso/git/test/undefined/test.cjs:1
require('./app.js')
^

, is generated?

I thought it was

// Print (filename):(line number): (message).

but I'm wrong

Found it. https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js#L1656

@mubashir-zenkoders
Copy link

Getting Same Error:

Here are my logs:

> [email protected] start:dev PROJECT_PATH
> nest start --watch

>  SWC  Running...
Successfully compiled: 115 files with swc (179.97ms)
Watching for file changes.

⠋  TSC  Initializing type checker...node:internal/modules/esm/loader:352
      throw new ERR_REQUIRE_ESM(url, true);
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module FILE_PATH/node_modules/.pnpm/[email protected]/node_modules/reflect-metadata/Reflect.js not supported.
    at TracingChannel.traceSync (node:diagnostics_channel:315:14) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.11.0

My NextJS Structure:

  1. Fastify
  2. SWC
  3. TypeORM
  4. PostgreSQL

@dario-piotrowicz
Copy link
Contributor

dario-piotrowicz commented Jan 25, 2025

It looks like this issue got fixed in 22.12.0 (and it is still fixed in v23)

See:
screenshot of issue reproduction using v22.11.0
screenshot of correct behavior using v22.12.0

So I think that this can safely be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants