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

436
Visualizações
Importing in Node.js: error "Must use import to load ES Module"

I'm trying to import myArr from hello.js into index.js. However I get an error of

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

File hello.js

export let myArr = ['hello', 'hi', 'hey'];

File index.js

import { myArr } from './hello.js';
console.log(myArr);

Where am I going wrong?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use version 2:

npm install node-fetch@2

node-fetch from v3 is an ESM-only module - you are not able to import it with require().

If you cannot switch to ESM, please use v2 which remains compatible with CommonJS. Critical bug fixes will continue to be published for v2.

over 4 years ago · Santiago Trujillo Relatório

0

I ran your code without any problems. Check for two things:

  1. Node.js version >= 14. It only works with the latest version of Node.js.
  2. Make sure your package.json includes a line for "type": "module". Without this line, Node.js assumes you want to use CommonJS modules rather than ESM.
over 4 years ago · Santiago Trujillo Relatório

0

I ran into a similar issue while building my React project.

Here's the error:

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/pradeep/Work/my_project/node_modules/@babel/runtime/helpers/interopRequireWildcard/_index.mjs

Then I realized that I am on a different Node.js version than the one used to install packages in this project.

I had two options:

  1. Change Node.js version to the one required by this project and build again.
  2. Stay on the Node.js version you have and remove the node_modules directory and package-lock.json file and do npm install again.

I chose the first approach and it worked for me.

over 4 years ago · Santiago Trujillo Relatório
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