react app build fails with 'ERR_REQUIRE_ESM' – Node.js

by
Ali Hasan
es6-modules node.js npm reactjs

The Solutions:

Solution 1: Force Downgrade strip-ansi Dependency

Add a “resolutions” or “overrides” field to your package.json to force the version of `strip-ansi` down to 6.0.0 or 6.0.1, where it is not pure ESM.

// package.json
{
  ...
  "resolutions": {
    "strip-ansi": "6.0.0"
  }
}
// package.json
{
  ...
  "overrides": {
    "strip-ansi": "6.0.0"
  }
}

Q&A

I am seeing ERR_REQUIRE_ESM while running build command for react app. How can I fix this?

Force the strip-ansi package down to version 6.0.0 or 6.0.1 in your package.json.

What could be causing this error and how can I prevent it?

This error can be caused by a change in the strip-ansi package to pure ESM in version 7.0.0. You can prevent it by forcing the package down to version 6.0.0 or 6.0.1 in your package.json.

Video Explanation:

The following video, titled "Deploy React App using Netlify and Fix process env CI = true Failed ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... React App using GitHub and Netlify. Also learn how to fix the common process env CI = true error that happens during npm run build. GitHub ...