我试图在我的项目中添加一个模块,在文档之后,我将这一行添加到我node.js项目的node.js中
import { bkLabs } from '@berkelium/nlp-core';但是我得到了以下错误
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然后我寻找了一个解决方案,我发现我必须将导入更改为一个需求。
const bkLabs = require('@berkelium/nlp-core');但是我得到了这个错误:
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.发布于 2022-06-13 13:50:01
看起来,您要使用的库是用ESModule语法编写的,而NodeJS代码是用CommonJS编写的。
不能要求() ESM脚本;只能导入ESM脚本,比如:从'foo‘导入{foo}。
这是CommonJS和ESModules混合的一个问题。默认情况下,nodejs使用CommonJS模块语法,除非在package.json中指定"type": "module"或使用.mjs表达式。
解决方案,选择对您最有效的方案:
@berkelium/nlp-core提交特性PR以支持混合模块语法(请参阅这里的更多信息:https://nodejs.org/api/packages.html#dual-commonjses-module-packages)发布于 2022-11-27 15:15:51
我是那个图书馆的开发者。很抱歉给您带来不便。在使用ESModule语法开发和打包库时,您会遇到这个问题。(即您必须使用import导入模块)。但是,当您试图在CommonJS语法项目中使用库时,它将无法工作。Evgheni Calcutin提供的答案和解决方案是正确的。
为了解决这个问题,我对库进行了重新处理,使其既支持ESModule语法又支持CommonJS语法。为了便于发音,图书馆改名了。因此,新的库是氦气。提供所有文件。
谢谢!
https://stackoverflow.com/questions/72602472
复制相似问题