我在我的 next.js 应用中添加了新的 4.0 openai
包,我仅在应用的服务器端使用它,但在构建时遇到了错误。
- info Linting and checking validity of types .Failed to compile../node_modules/openai/src/core.ts:539:3Type error: Private identifiers are only available when targeting ECMAScript 2015 and higher. 537 | 538 | export abstract class AbstractPage<Item> implements AsyncIterable<Item> {> 539 | #client: APIClient; | ^ 540 | protected options: FinalRequestOptions; 541 | 542 | protected response: Response;
我尝试将 tsconfig
的目标改为 es6、es2015、esnext,但似乎不起作用,skipLibCheck
已启用,所以我更加困惑为什么会发生这种情况
这是我的 tsconfig:
{ "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ { "name": "next" } ], "paths": { "$/*": [ "./src/*" ], "$app/*": [ "./src/app/*" ], "$cmp/*": [ "./src/components/*" ], "$types/*": [ "./src/types/*" ], "$assets/*": [ "./src/assets/*" ], "$lib/*": [ "./src/lib/*" ], } }, "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts" ], "exclude": [ "node_modules" ]}
回答:
使用这个导入(导入中没有“src”):
import { type ChatCompletionMessageParam } from "openai/resources/index.mjs";
而不是:
import { ChatCompletionMessageParam } from "openai/src/resources/chat/completions";