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

218
Vistas
Import es module file from CommonJS TypeScript

I'm trying to import a package where its package.json specifies "type": "module". I saw Can Typescript import CommonJS Modules?, but its solution (--allowJs) does not work for me.

I've tried setting various values in my tsconfig.json for module, moduleResolution, allow allowJs. Both esModuleInteropt and allowSyntheticDefaultImports are set to true.

I've tried using import ... from, import(...).then(), import ... = require(...), and renaming my index.js and the package's index.js to index.cjs. They all result in the same error when I run in node:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\src\...\node_modules\stringify-object\index.js
require() of ES modules is not supported.
require() of E:\src\...\node_modules\stringify-object\index.js from E:\src\...\index.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 E:\src\...\node_modules\stringify-object\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\src\...\node_modules\stringify-object\package.json.

I tried removing "type": "module" from the packages's package.json, but it relies on other packages with "type": "module".

The last time I tried to figure this out, I gave up and went with a different package. This really doesn't seem like it should be hard to do.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found what I'd consider a workaround. If I set "module": "ESNext" in tsconfig.json and rename the output to index.mjs, it works. It's not particularly elegant, though. There just must be a better way.

I'd be happy to accept a different, better answer.


As pointed out in the comment, setting "type": "module" in my project's package.json makes it so I do not need to rename index.js to index.mjs.

about 4 years ago · Juan Pablo Isaza 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