【问题标题】:SyntaxError: Cannot use import statement outside a module on node.jsSyntaxError:无法在 node.js 上的模块外部使用 import 语句
【发布时间】:2022-06-13 20:00:13
【问题描述】:

我正在尝试将一个模块添加到我的项目中,按照文档,我将此行添加到我的 index.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.

【问题讨论】:

标签: javascript node.js


猜你喜欢
  • 2023-01-31
  • 2022-11-02
  • 2022-01-25
  • 2020-08-25
  • 2020-10-10
  • 2020-05-02
  • 1970-01-01
相关资源
最近更新 更多