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

671
Views
SyntaxError: no se puede usar la declaración de importación fuera de un módulo en node.js

Estoy tratando de agregar un módulo a mi proyecto, siguiendo el documento, agrego esta línea a mi index.js en mi proyecto node.js

 import { bkLabs } from '@berkelium/nlp-core';

pero me sale el siguiente error

 SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:1001:16) at Module._compile (internal/modules/cjs/loader.js:1049:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12) at internal/main/run_main_module.js:17:47

luego busqué una solución y descubrí que tengo que cambiar la importación por un requerimiento

 const bkLabs = require('@berkelium/nlp-core');

pero luego me sale este error:

 internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/src/index.js require() of ES modules is not supported. require() of /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/src/index.js from /home/user/proyectos/chatBot/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 /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/src/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/package.json.
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Parece que la biblioteca que intenta usar está escrita con la sintaxis ESModule y su código NodeJS con CommonJS.

No puede requerir () secuencias de comandos ESM ; solo puede importar secuencias de comandos ESM, así: importar {foo} desde 'foo'.

Este es un problema al mezclar CommonJS y ESModules. De manera predeterminada, nodejs usa la sintaxis del módulo CommonJS a menos que especifique "type": "module" en package.json o use .mjs .

Solución, elige la que mejor te funcione:

  1. Escriba NodeJS en la sintaxis de ESModule
  2. Envíe la característica PR a @berkelium/nlp-core para admitir la sintaxis del módulo híbrido (vea más información aquí: https://nodejs.org/api/packages.html#dual-commonjses-module-packages )
about 4 years ago · Santiago Gelvez Report

0

la solución que ha buscado es correcta solo cuando está trabajando con archivos node.js , pero si desea importar módulos como un ES, debe especificar "type": "module" en package.json

about 4 years ago · Santiago Gelvez Report
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!