How would you fix an 'ERR_REQUIRE_ESM' error?
Asked Answered
F

5

19

I am trying to use the chalk npm. My code is:

     const chalk = require('chalk');

          console.log(
          chalk.green('All sytems go') +
          chalk.orange('until').underline +
          chalk.black(chalk.bgRed('an error occurred'))
           );

And I receive this error in my terminal when I type node main.js

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/ezell/Documents/CodeX/NPM/node_modules/chalk/source/index.js from /Users/ezell/Documents/CodeX/NPM/main.js not supported.
Instead change the require of index.js in /Users/ezell/Documents/CodeX/NPM/main.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/ezell/Documents/CodeX/NPM/main.js:1:15) {
  code: 'ERR_REQUIRE_ESM'
}
Forcer answered 9/4, 2022 at 2:32 Comment(0)
P
5

You need to switch to using the import keyword, as Chalk 5 only supports ESM modules.


So, to fix your code to adapt these changes, you need to...

  1. Edit your package.json file to allow ESM imports. Add the following in your package.json file:

    {
      "type": "module"
    }
    
  2. Load Chalk with the import keyword, as so:

    import chalk from "chalk";
    

If you, however, want to use require(), you need to downgrade to Chalk 4. Follow these steps to downgrade.

  1. Replace your existing chalk key with the following in your package.json file:

    {
      "dependencies": {
        "chalk": "4.1.2"
      }
    }
    
  2. Then, run the following command to install Chalk from your package.json file. Make sure to run this in the directory in which your package.json file is in!

    $ npm install
    
  3. Use the require() statement like normal.

    const chalk = require("chalk");
    

In summary, these are the two things you can do.

  • Stay with Chalk 5, and update import statements.
  • Downgrade to Chalk 4, and keep require() statements.
Proponent answered 9/4, 2022 at 3:58 Comment(3)
Thank you so much! Adding the import chalk from "chalk"; and "type": "module" worked!!Forcer
"add the following" OK, where at the end of the file?Mcewen
@Mcewen It doesn't matter where.Proponent
S
18

I got the same 'ERR_REQUIRE_ESM' error for nanoid:^4.0.0 & there are multiple ways to resolve this error:-

1)Use fix esm https://www.npmjs.com/package/fix-esm module & import the module like this:

const someModule = require("fix-esm").require("some-module");

2)Use dynamic import as shown below:

import('nanoid') 
.then((res)=>{ console.log(res) })         
.catch((err)=>{ console.log(err) });

Just make sure you dont have type:"module" field in package.json in above both cases otherwise you got "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension" error

3)Downgrade the module version to a stable old version, for eg in my case it was resolved when I downgraded nanoid version to :

"nanoid": "^3.1.22"
Snowstorm answered 7/8, 2022 at 8:31 Comment(3)
fix-esm is the way to go. Should happen by default.Troy
fix-esm is a lifesaver.. wish I could have learned about earlier.. much headache would have been prevented ESM can kiss my assCarrick
fix-esm fixed the issue for not just this package but all of them. This package is a lifesaver.Labefaction
L
7

The latest version of Chalk is only compatible with ESM modules and thus wants you to load it with import, not require().

From the doc:

IMPORTANT: Chalk 5 is ESM. If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now. Read more.

So, your choices are:

  1. Switch your project to an ESM module and load the latest version of Chalk with import instead of require().

  2. Install version 4 of Chalk which can be used with require().

  3. With a fairly recent version of Node.JS, you can use dynamic import to load the ESM module into your CommonJS module: const chalk = await import('chalk');

Lariat answered 9/4, 2022 at 3:22 Comment(0)
P
5

You need to switch to using the import keyword, as Chalk 5 only supports ESM modules.


So, to fix your code to adapt these changes, you need to...

  1. Edit your package.json file to allow ESM imports. Add the following in your package.json file:

    {
      "type": "module"
    }
    
  2. Load Chalk with the import keyword, as so:

    import chalk from "chalk";
    

If you, however, want to use require(), you need to downgrade to Chalk 4. Follow these steps to downgrade.

  1. Replace your existing chalk key with the following in your package.json file:

    {
      "dependencies": {
        "chalk": "4.1.2"
      }
    }
    
  2. Then, run the following command to install Chalk from your package.json file. Make sure to run this in the directory in which your package.json file is in!

    $ npm install
    
  3. Use the require() statement like normal.

    const chalk = require("chalk");
    

In summary, these are the two things you can do.

  • Stay with Chalk 5, and update import statements.
  • Downgrade to Chalk 4, and keep require() statements.
Proponent answered 9/4, 2022 at 3:58 Comment(3)
Thank you so much! Adding the import chalk from "chalk"; and "type": "module" worked!!Forcer
"add the following" OK, where at the end of the file?Mcewen
@Mcewen It doesn't matter where.Proponent
H
3

Solution, this happens because you have to use the current stable release 2.x first:

npm uninstall -D node-fetch

After that:

npm install node-fetch@2

This should work.

Headwards answered 9/8, 2022 at 16:18 Comment(1)
I really wished there was more explanation to this "solution".Usher
U
0

I had this error with mocha chai. I updated my package.json to an older version of chai and did an 'npm install' afterwards. Try updating to an older version.

Unwish answered 10/4 at 23:35 Comment(3)
Would you explain how exactly you updated your package.json? Was this by hand or did you use npm install [email protected] or npm update?Oust
I did it manually in the package.json then I did a npm install in my terminal after. There is a Youtube video out there. Wish I could find it. These are my final devDependencies for reference: "chai": "^4.3.7", "chai-http": "^4.4.0", "mocha": "^10.4.0".Unwish
Next time use a npm command instead of manually changing the package.json.Oust

© 2022 - 2024 — McMap. All rights reserved.