'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

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 1:[1]

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'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Scott