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

[BUG] ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time #7857

Open
2 tasks done
alexsch01 opened this issue Oct 18, 2024 · 25 comments
Labels
Bug thing that needs fixing Needs Triage needs review for next steps

Comments

@alexsch01
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

When installing any package with Node.js v23.0.0 and npm v10.9.0, I get the following warning

(node:10424) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

Expected Behavior

No response

Steps To Reproduce

No response

Environment

  • npm: 10.9.0
  • Node.js: 23.0.0
  • OS Name: Windows 10 22H2
  • npm config: default
; node bin location = C:\Program Files\nodejs\node.exe
; node version = v23.0.0
; npm local prefix = C:\Users\$USER\Downloads\test
; npm version = 10.9.0
; cwd = C:\Users\$USER\Downloads\test
; HOME = C:\Users\$USER
; Run `npm config ls -l` to show all defaults.
@alexsch01 alexsch01 added Bug thing that needs fixing Needs Triage needs review for next steps labels Oct 18, 2024
@shadowspawn
Copy link
Contributor

This is triggered by the experimental --experimental-require-module flag being made the default behaviour in Node.js 23. Related links:

@doni108108doni doni108108doni mentioned this issue Oct 19, 2024
2 tasks
@MikeMcC399
Copy link
Contributor

It's not necessary to install anything with npm to provoke the ExperimentalWarning.

For example, even with no npm modules installed, executing npm view npm version results in the ExperimentalWarning:

$ npm view npm version
(node:5212) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
10.9.0

Ubuntu 24.04.1 LTS
Node.js v23.0.0
npm 10.9.0 (latest)

Same ExperimentalWarning for many other CLI commands from https://docs.npmjs.com/cli/v10/commands:

npm audit
npm ci
npm doctor
npm init
npm install
npm install-ci-test
npm ls
npm outdated
npm prune
npm repo
npm show
... etc

@MikeMcC399
Copy link
Contributor

@MikeMcC399
Copy link
Contributor

Setting the environment variable NODE_OPTIONS='--trace-warnings' produces some trace output.

$ NODE_OPTIONS='--trace-warnings' npm view npm view
(node:6610) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time
    at emitExperimentalWarning (node:internal/util:273:11)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1388:5)
    at Module._compile (node:internal/modules/cjs/loader:1525:5)
    at Object..js (node:internal/modules/cjs/loader:1680:16)
    at Module.load (node:internal/modules/cjs/loader:1328:32)
    at Function._load (node:internal/modules/cjs/loader:1138:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
    at Module.require (node:internal/modules/cjs/loader:1350:12)
    at require (node:internal/modules/helpers:138:16)

@RedYetiDev
Copy link

FYI the ESM being require()d is supports-color.

@MikeMcC399
Copy link
Contributor

@RedYetiDev

FYI the ESM being require()d is supports-color.

You're right!

"supports-color": "^9.4.0",

where supports-color@9.0.0 and above is pure ESM

const supportsColor = require('supports-color'); 

where debug is effectively allowing an ESM version of supports-color to be installed and used:

  "peerDependenciesMeta": {
    "supports-color": {
      "optional": true
    }

So I guess the underlying issue debug-js/debug#975 needs to be resolved by debug.

@MikeMcC399
Copy link
Contributor

MikeMcC399 commented Oct 19, 2024

npm install npm@9.7.1 -g

and there was no more warning under Node.js v23.0.0 from

npm view npm version

rnazmo added a commit to rnazmo/my-web-tools that referenced this issue Oct 21, 2024
Why?:
Because the following error will happen when running server.

```bash
$ npm run dev

> my-web-tools@0.0.0 dev
> vite

  VITE v5.4.9  ready in 310 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help
(node:59826) ExperimentalWarning: Support for loading ES Module in
require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
file:///home/toor/repos/github.com/rnazmo/my-web-tools/tailwind.config.js:56
  plugins: [require("tailwindcss-animate")],
           ^

ReferenceError: require is not defined
    at
file:///home/toor/repos/github.com/rnazmo/my-web-tools/tailwind.config.js:56:12
    at ModuleJobSync.runSync
(node:internal/modules/esm/module_job:367:35)
    at ModuleLoader.importSyncForRequire
(node:internal/modules/esm/loader:325:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1392:24)
    at Module._compile (node:internal/modules/cjs/loader:1525:5)
    at Object..js (node:internal/modules/cjs/loader:1680:16)
    at Module.load (node:internal/modules/cjs/loader:1328:32)
    at Function._load (node:internal/modules/cjs/loader:1138:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
    at Module.require (node:internal/modules/cjs/loader:1350:12)
    at require (node:internal/modules/helpers:138:16)
    at
/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/tailwindcss/lib/lib/load-config.js:54:27
    at loadConfig
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/tailwindcss/lib/lib/load-config.js:58:6)
    at getTailwindConfig
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/tailwindcss/lib/lib/setupTrackingContext.js:71:116)
    at
/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/tailwindcss/lib/lib/setupTrackingContext.js:100:92
    at
/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/tailwindcss/lib/processTailwindFeatures.js:46:11
    at plugins
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/tailwindcss/lib/plugin.js:38:69)
    at LazyResult.runOnRoot
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/postcss/lib/lazy-result.js:329:16)
    at LazyResult.runAsync
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/postcss/lib/lazy-result.js:258:26)
    at LazyResult.async
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/postcss/lib/lazy-result.js:160:30)
    at LazyResult.then
(/home/toor/repos/github.com/rnazmo/my-web-tools/node_modules/postcss/lib/lazy-result.js:404:17)

Node.js v23.0.0
```

What I did:

```bash
$ mise use node@22
✔mise ~/repos/github.com/rnazmo/my-web-tools/.mise.toml tools:
node@22.9.0
$ node --version
v22.9.0
```

Ref:
    npm/cli#7857
    https://mise.jdx.dev/cli/use.html

See also:
    https://tailwindcss.com/docs/configuration
rnazmo added a commit to rnazmo/vite-react-ts-tailwind-template that referenced this issue Oct 21, 2024
What I did:

```bash
$ mise --version
2024.10.7 linux-x64 (7d15bd5 2024-10-14)

$ mise ls-remote node | tail -n 5
22.7.0
22.8.0
22.9.0
22.10.0
23.0.0

$ mise use node@22
mise ~/repos/github.com/rnazmo/vite-react-ts-tailwind-template/.mise.toml tools: node@22.9.0

$ node --version
v22.9.0
````

NOTE:
    Use 22 not 23 because 23 is still new and will cause trouble.

Ref:
    https://mise.jdx.dev/demo.html
    https://mise.jdx.dev/lang/node.html
    https://mise.jdx.dev/cli/ls-remote.html
    https://mise.jdx.dev/cli/use.html
    npm/cli#7857
@MikeMcC399
Copy link
Contributor

MikeMcC399 commented Oct 25, 2024

Under Node.js v23.1.0 the warning provides additional debugging information which confirms the previous analysis:

$ npm view npm version
(node:13088) ExperimentalWarning: CommonJS module /home/mike/n/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /home/mike/n/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
10.9.0

@deleonjulio
Copy link

Out of nowhere, npm install and npm start doesnt work in my project. I have to downgrade my node version to make it work.

@MikeMcC399
Copy link
Contributor

@deleonjulio

Out of nowhere, npm install and npm start doesnt work in my project. I have to downgrade my node version to make it work.

That sounds like a different issue. This issue is about an ExperimentalWarning only, where npm continues to work.

@SparK-Cruz
Copy link

What can we do other than rewrite everything between npm to debug, inclusive, in ESM?

@ljharb
Copy link
Contributor

ljharb commented Oct 30, 2024

@SparK-Cruz you can't do anything, whatsoever. Only node, npm, or debug, can do something about it.

@LitoMore

This comment was marked as off-topic.

@SparK-Cruz
Copy link

SparK-Cruz commented Oct 30, 2024

@SparK-Cruz you can't do anything, whatsoever. Only node, npm, or debug, can do something about it.

@ljharb We can always open a pull request... if gets approved or not is another story. Worst case scenario I can fork it and add it to the AUR.

@ljharb
Copy link
Contributor

ljharb commented Oct 30, 2024

@SparK-Cruz yes, a PR to debug would be a possible path forward.

Forking things to AUR without the consent of the maintainer doesn’t actually help the ecosystem, though, and it makes things worse for AUR users because they’re not really using the actual software.

zloirock added a commit to zloirock/core-js that referenced this issue Oct 30, 2024
@mantoni
Copy link
Contributor

mantoni commented Oct 31, 2024

Hm, I think the cause of this is npm cli directly depending on a different major version of supports-color than chalk and debug. I tried downgrading to v7.x in #7887 and it fixes the problem and seems to work fine.

@alexsch01 alexsch01 reopened this Oct 31, 2024
@wellwelwel
Copy link

wellwelwel commented Nov 4, 2024

I opened a PR with a really simple approach to fix it from supports-color side, by adding an automation to also export the index.js (ESM) to index.cjs. This allows the supports-color to support both CommonJS, ES Modules and browsers, fixing the issue in both debug and npm/cli, and allowing Node.js to keep the experimental warning.

Also, no changes would be necessary either on the debug or npm/cli side (except maybe forcing an update to the new supports-color version, if approved).


EDIT: Unfortunately, supports-color's decision is in fact not to be compatible with CommonJS.

@damisparks
Copy link

I get this error on npm publish:

(node:85003) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
npm error code ENOENT
npm error syscall open

I was only able to publish by using node 22.

@ljharb
Copy link
Contributor

ljharb commented Nov 4, 2024

@damisparks node v23.0.0 is broken, so you'd need to be on v23.1.0. The experimental warning would still occur, though.

@damisparks
Copy link

@ljharb After upgrading to v23.1.0 , I could publish.
I still see the experimental warning, as you said.

(node:19390) ExperimentalWarning: CommonJS module /Users/damisparks/.nvm/versions/node/v23.1.0/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /Users/damisparks/.nvm/versions/node/v23.1.0/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

@wellwelwel

This comment has been minimized.

@jonkoops
Copy link

jonkoops commented Nov 6, 2024

Considering the CLI targets Node.js versions that all support ESM, perhaps the easiest way to mitigate this issue is to start using ES modules instead of CommonJS? Is there any particular blocker that might prevent doing so?

@ljharb
Copy link
Contributor

ljharb commented Nov 6, 2024

@jonkoops the issue occurs in debug, which is CJS, so npm using ESM would have no effect on it whatsoever.

@jonkoops
Copy link

jonkoops commented Nov 6, 2024

@ljharb I see you already logged debug-js/debug#975 for that, thanks I'll follow the discussion there. Still, this would presumably also affect the CLI even if this debug itself was ESM, but just move the warning up the chain.

So my question still stands, would there be any technical reason the CLI itself could not be authored in ESM in terms of compatibility?

@ljharb
Copy link
Contributor

ljharb commented Nov 6, 2024

Probably not? but there'd also be no concrete benefit, either - only a subjectively ideological one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps
Projects
None yet
Development

No branches or pull requests