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

250
Vistas
Move from CommonJS to ESM's export/import

It might has been fully discussed but I can't find a perfect answer from either here or here.

Basically

  • CommonJS uses the require('./xx') syntax, while
  • ESM uses the import {stuff} from './xx' syntax

Here is how I've been doing the require when using CommonJS

  • require('apackage') or
  • require('./myfile')

I.e., I don't need to specify their extensions for either case.

But moving to ESM import, the import {stuff} from './myfile' is giving me the error of:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module

In here it shows how to move from CommonJS and do ESM import:

from

const colors = require("colors");         // Obj w/ all export fields
const { red, blue } = require("colors");  // Select fields from export obj

to

import colors from "colors";          // _Only_ `default` export (`{ green }`)
import { red, blue } from "colors";   // Other named exports

But is it possible for me not to specify their extensions for my own files as well? Otherwise,

  • it will be inconsistent between importing 'apackage` or './myfile',
  • lots of unnecessary work for me to add their extensions to my current imports
  • and need to force me to change again if I decided to switch their extensions between js and mjs,
about 4 years ago · Juan Pablo Isaza
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