Search code examples
node.jsreactjsnext.jsopenai-api

next.js 13 build error for private identifiers when using openai 4.0 package


I added the new 4.0 openai package in my next.js app, i'm using it in the server of the app only and when building i get the error.

- info Linting and checking validity of types .Failed to compile.

./node_modules/openai/src/core.ts:539:3
Type 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;


I tried changing the tsconfig target to es6,es2015,esnext, but it doesn't seem to work, skipLibCheck is enabled so i'm even more confused as of why it happens

This is my 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"
  ]
}

Solution

  • I had exactly the same problem, and I solved it by deleting the imports with openai types. In my case, it was

    import { 
      ChatCompletionSystemMessageParam,
      ChatCompletionUserMessageParam,
     } from "openai/src/resources/chat/completions";