chalk - Error [ERR_REQUIRE_ESM]: require() of ES Module

Chalk 5 has changed to ESM. They provide a link to better understand what that means: Pure ESM.

From chalk README:

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.

As of this reply, the last version of chalk 4 is 4.1.2.


That is something to do with the version you are using which is I think 5.0.0

  1. npm uninstall chalk

then

  1. npm i [email protected]

now you can run your code

const chalk = require('chalk');
console.log(chalk.blue('Hello world!')); 

//First Change in you Package.json

"main": "app.js",
"type": "module",//use this line 

//Second change in App.js

import os from 'os' //  const os = require("os")

import chalk from 'chalk';//const chalk = require("chalk")

Step-1 npm uninstall chalk (delete all chalk files)


Step-2 npm install [email protected]

const chalk = require("chalk");
console.log(chalkl.green("Hello"));

Done🙌🏻