Borislav Hadzhiev
Fri Feb 18 2022·1 min read
Photo by Allef Vinicius
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.
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.
npm install got@11.8.3
Now you are able to use the got
package with the require()
syntax.
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.
If your environment supports ES Modules, you should try to convert the
require()
import to ESM.
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.