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'
}
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With