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

219
Visualizações
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 Respostas
Responde à pergunta

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 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