In this article, you will learn how to fix the error: [ERR_REQUIRE_ESM]: require() not supported In JavaScript. Let’s follow this article to learn how to solve this error with the explanation and examples below.
How does the error “[ERR_REQUIRE_ESM]: require() not supported” in JavaScript occur?
This error occurs when the package you try to import can’t be imported by the require() method anymore. The package you try to import can be converted to an ESM-only package.
Look at the example below to learn more about this error.
const foo = require(‘foo’); // This Error will occur here
Output
Error [ERR_REQUIRE_ESM]: require() of ES module
This Error will continue to occur if you still try to use the require() function. Follow up the next title to learn how to fix the error: [ERR_REQUIRE_ESM]: require() not supported In JavaScript.
How to fix this error?
To fix this error, you must update the version you are using, the node.js 14 or the newer version than this. But you can solve this error without updating the new version by adding the line “type”: “module” in the package.json file. It will be helpful to you.
Add the following line to the package.json file: “type”: “module”
To solve the error: [ERR_REQUIRE_ESM]: require() not supported in JavaScript. You can add a line: “type”: “module” to the json file in your folder.
{ // The old content will be here "type": "module" }
After adding this line to the json file, you must use the import method instead of the require() method.
import foo from 'foo';
Now you can use the module that you need in this way.
Use the node.js 14 version or the newer version.
You can use the node.js version or the newer version to solve this error. It will solve the error.
First, you must install nvm by using the command below in your terminal.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
After that, install the specific node version using nvm.
nvm install 16.17.1
If you get into trouble when installing this version, see here.
This is the latest version now.
After doing this, you can fix the error: [ERR_REQUIRE_ESM]: require() not supported.
For older version
Suppose you are using a version older than the 14 version. This way will help you by the following command.
node --experimental-modules your.mjs
Summary
These are some ways that can help you fix the error: [ERR_REQUIRE_ESM]: require() not supported in JavaScript. You can add the line: “type”: “module” to the json file, use the node.js 14 or newer version, or use another solution above for the older version. Choosing the solution is the best for your status. We hope this article is helpful for you. Thanks!
Maybe you are interested:

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R