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

247
Views
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
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!