. Advertisement .
..3..
. Advertisement .
..4..
Have you ever encountered the “[ERR_REQUIRE_ESM]: require() of ES Module from not supported” error? If yes, let’s follow this article. We will help you understand the cause of this problem and some solutions to fix it. Read on it.
How Does The Error “[ERR_REQUIRE_ESM]: require() of ES Module from not supported” Happen?
When you are attempting to run your nodejs app, you will get this error:
[ERR_REQUIRE_ESM]: require() of ES Module from not supported
How To Solve The Error “[ERR_REQUIRE_ESM]: require() of ES Module from not supported”?
Solution 1: Use import rather than require()
If you use node-fetch v3, require() is no longer supported when importing. You must use import something from something if you want to import anything. You only need to use import in place of require() like the following example to resolve the “[ERR_REQUIRE_ESM]: require() of ES Module from not supported” error.
import fetch from "node-fetch";
Furthermore, you must include “type”: “module” in your package.jsonas shown below:
{
….
"type": "module",
….
}
Solution 2: Use the await import(…) method
The import(…) function is supported by NodeJs’ CommonJS scheme for dynamic importing. Look at the following example:
// ESM import
import boxen from 'boxen';
// Change to dynamic import method
const { default: boxen } = await import('boxen');
However, all dynamic imports are really compiled as the require(…) method because tsc compiles the output as CommonJS.
Solution 3: Downgrade the node-fetch version
Immediately after the release of node-fetch version 3, they discontinued supporting require() in importing. You can fix your problem by downgrading node-fetch to version 2.6.1 or 2.6.6, which allows us to use the require() syntax.
To downgrade the node-fetch version, open your terminal and run this command.
npm install [email protected]
OR
npm install [email protected]
After that, you can use require() without encountering any problems while importing something.
Solution 4: Change your project to an ESM solution
Change your project to an ESM solution if at all possible. For example, replace “module”: “CommonJS” in tsconfig.json with “module”: “ESNext”. However, due to the significant differences between the ESM and CommonJS, the actual code logic may also require significant adaptation changes.
Conclusion
The solutions mentioned above are the most effective solutions for the error “[ERR_REQUIRE_ESM]: require() of ES Module from not supported“. We believe you can quickly resolve your problem with these methods. In addition, if you still need help or have problems, please leave your comment below. We are always willing to help you. Thank you for your reading!
Read more
Leave a comment