简体   繁体   English

SyntaxError:无法在 node.js 上的模块外部使用 import 语句

[英]SyntaxError: Cannot use import statement outside a module on node.js

I'm trying to add a module to my project , following the doc I add this line to my index.js on my node.js project我正在尝试将一个模块添加到我的项目中,按照文档我将此行添加到我的node.js项目上的index.js

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

but I get the following 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

then I looked for a solution and I found that I have to change the import for a require然后我寻找了一个解决方案,我发现我必须更改导入以获得需求

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

but then I get this 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.

Looks like the library you're trying to use is written with ESModule syntax and your NodeJS code with CommonJS.看起来您尝试使用的库是用 ESModule 语法编写的,而您的 NodeJS 代码是用 CommonJS 编写的。

You can't require() ESM scripts ;不能 require() ESM 脚本 you can only import ESM scripts, like that: import {foo} from 'foo'.您只能导入 ESM 脚本,例如: import {foo} from 'foo'。

This is a problem with mixing CommonJS and ESModules.这是混合 CommonJS 和 ESModules 的问题。 By default, nodejs uses CommonJS module syntax unless you specify "type": "module" in package.json or using .mjs exentions.默认情况下,nodejs 使用 CommonJS 模块语法,除非您在package.json中指定"type": "module"或使用.mjs扩展。

Solution, choose one that works best for you:解决方案,选择最适合您的解决方案:

  1. Write NodeJS in ESModule syntax用 ESModule 语法编写 NodeJS
  2. Submit feature PR to @berkelium/nlp-core to support hybrid-module syntax (see more information here: https://nodejs.org/api/packages.html#dual-commonjses-module-packages )@berkelium/nlp-core提交功能 PR 以支持混合模块语法(在此处查看更多信息: https ://nodejs.org/api/packages.html#dual-commonjses-module-packages)

您查看的解决方案仅在您使用node.js文件时才是正确的,但是如果您想将模块作为 ES 导入,则必须在package.json中指定"type": "module"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js 获取不能在 node.js 中的模块外使用导入语句 - Get Cannot use import statement outside a module in node.js JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module 语法错误:不能在模块 discord.js 之外使用导入语句 - SyntaxError: Cannot use import statement outside a module discord.js 纯 JS - SyntaxError:不能在模块外使用 import 语句 - Pure JS - SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module 不能使用 jest 在模块 node.js 之外使用 import 语句 - Cannot use import statement outside a module node.js using jest Javascript 模块错误语法错误:不能在模块外使用导入语句 - Javascript Module error SyntaxError: Cannot use import statement outside a module 尝试使用 three.js 时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when trying to use three.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM