How to Fix "Error [ERR_REQUIRE_ESM]: require() of ES Module not supported" Error in Javascript?
The "Error [ERR_REQUIRE_ESM]: require() of ES Module not supported" error occurs when you try to use a require statement to import an ES module in a Node.js application that is not configured to support ES modules.
To fix this error, you can either:
- Use the import statement instead of require:
import module from './path/to/module.js';
Update your Node.js version to at least version 13.2.0, which includes support for ES modules by default.
Add the "type":"module" field to your package.json file, which tells Node.js to treat all .js files as ES modules:
{
"type": "module"
}
- Add the "--es-module-specifier-resolution=node" flag when running your Node.js application, which tells Node.js to use the default module resolution algorithm for ES modules:
node --es-module-specifier-resolution=node app.js
By following any one of these methods, you should be able to fix the "Error [ERR_REQUIRE_ESM]: require() of ES Module not supported" error in your Javascript project.