got Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

got Error [ERR_REQUIRE_ESM]: require() of ES Module not supported #

The got error "[ERR_REQUIRE_ESM]: require() of ES Module not supported" occurs because the got package has been converted to be an ESM only package in version 12, which means that the package cannot be imported with require() anymore.

got err require esm

To solve the got error "[ERR_REQUIRE_ESM]: require() of ES Module not supported", downgrade the version of the package to 11.8.3 by running the following command: npm install got@11.8.3. This is the last version of got that is built with CommonJS.

To install version 11.8.3 of got, open your terminal in your project's root directory and run the following command.

shell
npm install got@11.8.3

Now you can use the got package with the require() syntax.

index.js
const got = require('got'); async function makeRequest() { const { data } = await got .post('https://httpbin.org/anything', { json: { hello: 'world', }, }) .json(); console.log(data); } makeRequest();

And here is the output I get in my terminal when I run the code from the snippet.

got import success

If your environment supports ES Modules, you should try to convert the require() import to ESM.

index.js
import got from 'got'; async function makeRequest() { const { data } = await got .post('https://httpbin.org/anything', { json: { hello: 'world', }, }) .json(); console.log(data); } makeRequest();

But this might not work, depending on your setup.

Note that if you use TypeScript with a version less than 4.6, you should be staying on got version 11.8.3. You can read more about this in this GitHub issue.

If you use a bundler and want to use ES Modules, you have to make sure that the bundler supports ESM and you have to configure it.

Conclusion #

To solve the got error "[ERR_REQUIRE_ESM]: require() of ES Module not supported", downgrade the version of the package to 11.8.3 by running the following command: npm install got@11.8.3. This is the last version of got that is built with CommonJS.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.