Welcome to the Linux Foundation Forum!

Require problem with got

jorge.melnik Posts: 4
edited February 2022 in LFW212 Class Forum

Hello, I cant import got with require.
const got = require('got')

Can you help me with the problem?

consuming-service@1.0.0 dev /home/jmeln/cursos/node/LF/JSNSD/ch-7/consuming-service
fastify start -w -l info -P app.js

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/jmeln/cursos/node/LF/JSNSD/ch-7/consuming-service/node_modules/got/dist/source/index.js
require() of ES modules is not supported.
require() of /home/jmeln/cursos/node/LF/JSNSD/ch-7/consuming-service/node_modules/got/dist/source/index.js from /home/jmeln/cursos/node/LF/JSNSD/ch-7/consuming-service/routes/root.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/jmeln/cursos/node/LF/JSNSD/ch-7/consuming-service/node_modules/got/package.json.

at new NodeError (internal/errors.js:322:7)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:93:18)
at Object.<anonymous> (/home/jmeln/cursos/node/LF/JSNSD/ch-7/consuming-service/routes/root.js:2:13)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32) {



Best Answer

  • baocongchen
    baocongchen Posts: 15
    Answer ✓

    Version 12 of got only supports import. You need to install version 11 for require to work. npm install got@11.


  • jorge.melnik

    thanks !

  • davidmarkclements

    correct got has dropped support for CJS. The content has been update to install 11. In the future we'll be using fetch in the material since Node 18 will be providing a global fetch.

This discussion has been closed.


Upcoming Training