Search code examples
node.jschalk

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


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")

Solution

  • 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.