0

我安装了 Node 16.13.1,并使用“npm install -g openai”全局安装了 openai。我使用以下方法在脚本中导入了包:

const { Configuration, OpenAIApi } = require('openai')

然而,当我运行我的脚本时,我得到了这个错误:

PS D:\Projects\OpenAI-Chat> node conversation.js
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'openai'
Require stack:
- D:\Projects\OpenAI-Chat\conversation.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (D:\Projects\OpenAI-Chat\conversation.js:1:38)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'D:\\Projects\\OpenAI-Chat\\conversation.js' ]
}

我该如何解决?

4

1 回答 1

2

你必须在本地安装它

npm i openai

因为 Node.js 不会在全局文件夹中搜索本地项目的依赖项。全局安装适用于全局可执行文件及其依赖项。

于 2022-02-17T01:02:18.043 回答