Fix - got Error [ERR_REQUIRE_ESM]: require not supported

avatar

Borislav Hadzhiev

Fri Feb 18 20221 min read

Fix - got Error [ERR_REQUIRE_ESM]: require not supported #

The got error "[ERR_REQUIRE_ESM]: require() 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() 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 the root directory of your project and run the following command.

shell
npm install got@11.8.3

Now you are able to 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 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 configure it.

Use the search field on my Home Page to filter through my more than 1,000 articles.