Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

428
Visualizações
Js: Can't import object from external file with node-fetch imported

I have some credential saved in a file called secrets.js, in the same folder of the main.js file. In the secrets.js file I wrote this:

const secrets = {}
secrets.key = "..."
secrets.discord = "..."
secrets.cityName = "..."
module.exports = secrets

In the main.js file I wrote this:

const secrets = require('./secrets.js')
const fetch = require('node-fetch')

async function main(){
    console.log(secrets)
}

main()

If I execute the code i get this error:

internal/modules/cjs/loader.js:1216
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ./weather/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of ./weather/node_modules/node-fetch/src/index.js from ./weather/main.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 ./weather/node_modules/node-fetch/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1216:13)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at Object.<anonymous> (./weather/main.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14) {
  code: 'ERR_REQUIRE_ESM'
}

But if I comment this line const fetch = require('node-fetch'):

const secrets = require('./secrets.js')
//const fetch = require('node-fetch')

async function main(){
    console.log(secrets)
}

main()

then it works and I get the expected output:

{ key: '...', discord: '...', cityName: '...' }

It appears that importing node-fetch is the cause of the error. I am using the v16.10.0 of node. Can someone help me?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda