Hlep me I am new.TypeError: Configuration is not a constructor

'm encountering an error using the OpenAI Node.js SDK in my Discord bot’s gptModule.js file. The error is as follows:


TypeError: Configuration is not a constructor

Here’s the relevant code snippet:


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

const configuration = new Configuration({

apiKey: process.env.OPENAI_API_KEY,

});

const openai = new OpenAIApi(configuration);

I have verified the following:

  1. The OpenAI library is up to date and the import statements are correct.

  2. The OPENAI_API_KEY environment variable is set correctly.

  3. I am using Node.js version v22.11.0.

Additionally, I have tried:

  • Deleting the node_modules directory and package-lock.json file, and then reinstalling dependencies.

  • Running the code in different versions of the Node.js environment.

Despite these efforts, the issue persists. If anyone has experienced a similar problem or has suggestions for resolving this, I would greatly appreciate your help!

Welcome @e226051540

The latest version of the OpenAI package for TS/JS has a different instantiating method than your current one:

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 client.chat.completions.create({
    messages: [{ role: 'user', content: 'Say this is a test' }],
    model: 'gpt-4o',
  });
}

main();
2 Likes