Coding⏱️ 3 min read📅 2026-06-04

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

ES Module error fix for chalk installation

Quick Answer: Update require() to dynamic import() in your code, e.g. import chalk from 'chalk';

The Error [ERR_REQUIRE_ESM]: require() of ES Module is a common error that affects developers who use CommonJS modules with ES Modules.

This error occurs when the Node.js environment tries to load an ES Module with the 'require' function, which is not supported in ES Modules.

🔍 Why This Happens

  • The primary reason for this error is due to the difference between CommonJS and ES Modules. CommonJS modules use the 'require' function to import dependencies, while ES Modules use the 'import' statement.
  • Another possible cause is that the module being imported (in this case, chalk) has not been updated to support ES Modules.

🚀 How to Resolve This Issue

Enabling ES Module Support

  1. Step 1: Update the package.json file of your project to specify the type of module as 'module': "esm". This will enable ES Module support in your project.
  2. Step 2: If you are using a version of Node.js older than 14, update it to the latest version, which includes ES Module support by default.
  3. Step 3: For projects that cannot be updated immediately, consider using a transpiler like Babel to convert CommonJS code to ES Modules.

Using Dynamic Imports

  1. Step 1: Update the line where you import chalk to use dynamic imports: `import chalk from 'chalk';` or `const chalk = await import('chalk');`.
  2. Step 2: This will allow your project to load ES Modules without requiring explicit type declarations in your code.

🎯 Final Words

By following these steps, you should be able to resolve the Error [ERR_REQUIRE_ESM]: require() of ES Module and get your project up and running with chalk.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions