Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!