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

298
Views
¿Cómo puedo usar @babel/register (o alternativa) junto con ESM nativo en Node?

Estoy escribiendo el conjunto de pruebas para una aplicación web y necesito importar el JSX desde el origen a un entorno de prueba. Parte de este proceso usa @babel/register para transpilar correctamente mi JSX a JS estándar. Mi configuración de babel se parece a lo siguiente:

 { "presets": ["@babel/preset-env"], "plugins": [ "@babel/plugin-syntax-jsx", ["babel-plugin-transform-jsx-to-htm",{ "import": { "module": "htm/preact", "export": "html" }, "tag": "$$html" }] ] }

Y ejecuto con node -r @babel/register ...<unrelated test command here>

El problema con el que me encuentro es cuando uno de mis archivos fuente se importa de una biblioteca que solo ofrece salida ESM, no CJS. Aquí está el error con el que me encontré:

 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: <file path to non-working ESM module> require() of ES modules is not supported. require() of <file path to non-working ESM module> from <file path to source that imports it> 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 <esm file>.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from <file path to non-working ESM module>/package.json. at Module._extensions..js (internal/modules/cjs/loader.js:1080:13) at Object.newLoader [as .js] (/home/ryun/Projects/foobar/node_modules/pirates/lib/index.js:104:7) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object.<anonymous> (<file path to source that imports it>.jsx:1:1) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Module._compile (/home/ryun/Projects/foobar/node_modules/pirates/lib/index.js:99:24) at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

En la parte inferior de la página de documentos de @babel/register , se indica: Note: @babel/register does not support compiling native Node.js ES modules on the fly, since currently there is no stable API for intercepting ES modules loading. Debido a esto, creo que @babel/register es un no-go, pero no se ofrece ninguna solución. No puedo descifrar qué se supone que debo hacer allí además de bifurcar esa lib que estoy usando y hacer que exporte CJS solo por el bien de mi conjunto de pruebas.

over 4 years ago · Santiago Trujillo
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!