90

Hi tried to install chalk on my very simple app and then i got error:

Error [ERR_REQUIRE_ESM]: require() of ES Module my-file-is-here  and chalk\node_modules\chalk\source\index.js from my-file-is-here not supported.
Instead change the require of index.js in my-file-is-here to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (`my-file-is-here`) {
  code: 'ERR_REQUIRE_ESM'
}

thats my code:

const os = require("os")
const chalk = require("chalk")

console.log("app running")
Elad87
  • 941
  • 1
  • 3
  • 8

11 Answers11

139

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.

AJ Gray
  • 1,536
  • 1
  • 10
  • 9
106

That is something to do with the version you are using which is I think 5.0.0. Use chalk@4.1.2 instead

  1. npm uninstall chalk

then

  1. npm i chalk@4.1.2

now you can run your code

const chalk = require('chalk');
console.log(chalk.blue('Hello world!')); 
Green_Lab
  • 63
  • 1
  • 4
ady arabiat
  • 1,061
  • 1
  • 4
  • 6
17

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


Step-2 npm install chalk@4.1.0

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

Done

hane Smitter
  • 516
  • 3
  • 11
Meghan
  • 179
  • 5
  • 4
    A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it. – Tyler2P Feb 03 '22 at 16:25
  • 1
    Agree with the above, but a good explanation is provided in the answer by AJ Gray: https://stackoverflow.com/a/70425265/2701497. – Chrisuu Feb 28 '22 at 17:22
  • These steps solved the issue for me btw. – Chrisuu Feb 28 '22 at 17:22
17

To use chalk^5 instead 4

You can use dynamic import with chalk^5 (ESM version) in CJS

const chalk = import("chalk").then(m=>m.default);

// Option 1
async function main(){
  console.log((await chalk).gray(">", ...commands));
}

// Option 2
async function main2(){
  const _chalk = await chalk;
  console.log(_chalk.gray(">", ...commands));
}

Downgrading to v4 is not a long term solution. You should be using version v5 to be ready for ESM

HerberthObregon
  • 1,811
  • 19
  • 23
0

I simply used import chalk from "chalk" and added "type": "module" in the package.json file which enabled es6 module worked for me quite well.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

in any such case, use import chalk from 'chalk'; // but before using that first you need to do some minor changes // in the package.json file, change the type to module, for e.g. "type" : "module"

0

Just use version 4 not 5. Latest version has been moved to esm

necimye
  • 59
  • 1
  • 3
0
const chalk = await (await (eval(`import('chalk')`) as Promise<typeof import('chalk')>)).default;

You can try to use this one.

huypham
  • 184
  • 3
  • 8
-1

Think it's ES module and works in import way, but you can do something like this:

const { chalk } = require("chalk");

It worked for me when I worked with firebase in v8 style.

Archil Labadze
  • 4,049
  • 4
  • 25
  • 42
-1
  • Add "type": "module" to your package.json.
  • Replace "main": "index.js" with "exports": "./index.js" in your package.json.
  • Update the "engines" field in package.json to Node.js 12: "node": "^12.20.0 || ^14.13.1 || >=16.0.0".
  • Remove 'use strict'; from all JavaScript files.
  • Replace all require()/module.export with import/export.

Use only full relative file paths for imports: import x from '.'; → import x from './index.js';.

If you have a TypeScript type definition (for example index.d.ts), update it to use ESM imports/exports.

Optional but recommended, use the node: protocol for imports.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
-4

//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")
Rishu Singh
  • 11
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 19 '21 at 21:46