openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Can't build NextJS project with openai library. Getting: Type error: Private identifiers are only available when targeting ECMAScript 2015 and higher.

Open bardaxx opened this issue 1 year ago • 5 comments

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • [X] This is an issue with the Node library

Describe the bug

Getting this kind of error on build time on NextJS 14 and I don't know why

image

this is my tsconfig.json

{
  "compilerOptions": {
    "target": "ES2023",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

Thanks for help.

To Reproduce

  1. Install and use library on nextjs
  2. import something like import typoe { Message } from 'openai/resources/beta/threads/messages';

Code snippets

No response

OS

macOS

Node version

v22.3.0

Library version

4.52.3

bardaxx avatar Jul 08 '24 15:07 bardaxx

updates: if I remove every import from 'openai/resources/beta/****'; all works but the question is about using proper types. Any ideas?

bardaxx avatar Jul 08 '24 16:07 bardaxx

Hi @giacomoalonzi can you provide a simplified reproduction I can checkout and run locally?

My assumption is this an issue with either the project configuration or Next.js' automatic dependency bundling feature.

A reproduction will be helpful for me to dig deeper. Thanks!

Ethan-Arrowood avatar Jul 08 '24 18:07 Ethan-Arrowood

did u ended up finding out the solution

iranzithierry avatar Jul 18 '24 16:07 iranzithierry

updates: if I remove every import from 'openai/resources/beta/****'; all works but the question is about using proper types. Any ideas?

The same issue with you, I copied that interface and types to my code then remove that interface and type imported from 'openai/resources/xxx' then I can build now. But I think it's not the best way to fix it.

youling4438 avatar Jul 26 '24 08:07 youling4438

I had the same issue and was able to reproduce this with.a fresh project using the example in the readme as index.ts

OS: OSX Sonoma 14.5 Node: v22.5.1 OpenAI Library: 4.53.0

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});

async function main() {
  const chatCompletion = await openai.chat.completions.create({
    messages: [{ role: 'user', content: 'Say this is a test' }],
    model: 'gpt-3.5-turbo',
  });
}

main();

The error appeared when running npx tsc index.ts I reran as npx tsc and it complied the project after a minor modification.

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
});

async function main(): Promise<void> {
    const chatCompletion = await openai.chat.completions.create({
        messages: [{ role: "user", content: "Say this is a test" }],
        model: "gpt-4o-mini",
    });

    // Extract and log the message content with a type guard
    const messageContent = chatCompletion.choices[0].message.content;
    if (messageContent !== null) {
        console.log(messageContent);
    } else {
        console.error("Message content is null");
    }
}

main();

My understanding from the tsc readme is that passing a file will skip the tsconfig.ts which was generating the issue for me.

lugnut42 avatar Jul 26 '24 14:07 lugnut42

I reproduce similar issue by importing openai/src/resources/audio/speech

just remove the src may able build right now openai/resources/audio/speech

kerrickchan avatar Aug 17 '24 07:08 kerrickchan

I suspect the original report here is because of an openai/src import somewhere.

If anyone can reproduce this issue without an openai/src import, please open a new issue with a minimal reproduction so we can investigate!

RobertCraigie avatar Aug 19 '24 14:08 RobertCraigie