Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

380
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda