Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

chalk package issue in nodejs

**When I require to chalk package in node js like as **

index.js

 const  chalk  = require('chalk');
 console.log(chalk.bgRed.inverse("hello world"));
 console.log(chalk.blue.inverse('Hello') + ' World' + chalk.red('!'));

package.json

{
"name": "npm_mod",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"

},
"author": "rahul",
"license": "ISC",
"dependencies": {
"chalk": "^5.0.0",
"type": "module"
}
}

when I run code then show the issue and how to deal with this issue without using .mjs file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

internal/modules/cjs/loader.js:1089
  throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
  ^
 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: R:\24-01-22-nodeJs\npm_mod\node_modul

json.

>Solution :

The error above is because you’re using the Common.JS module require syntax rather than the ES module import syntax.

Update:

const chalk = require('chalk')

to be:

import chalk from 'chalk'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading