I 'm using Openai api in nodeJS but throwing error

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

// const readline = require(“readline”);

const configuration = new Configuration({
apiKey: “API_KEY”,
});

TypeError: Configuration is not a constructor
at Object. (D:\nodeJS-chatBot\index.cjs:5:23)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1280:10)
at Module.load (node:internal/modules/cjs/loader:1089:32)
at Module._load (node:internal/modules/cjs/loader:930:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47

1 Like

It looks like vaibhavambhore803 is having an issue while trying to integrate OpenAI’s API with a Node.js application. The error message indicates that Configuration is not being recognized as a constructor.

Here’s a possible solution to the problem, outlined step-by-step:

1. Install the OpenAI Package

Ensure that you have installed the OpenAI package for Node.js. You can do this by running the following command in your project directory:

bash

npm install openai

2. Import the OpenAI Library

The code snippet you provided seems to be using incorrect import syntax. Make sure to import the OpenAI library properly by using the following code:

javascript

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

3. Initialize the OpenAI API

Next, you can initialize the OpenAI API with your API key as follows:

javascript

const openai = new OpenAIApi({
  api_key: 'API_KEY'
});

Make sure to replace 'API_KEY' with your actual API key.

4. Check Your Code

Here’s a revised version of the code snippet:

javascript

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

const openai = new OpenAIApi({
  api_key: 'API_KEY'
});

// Rest of your code

5. Run Your Code

Now, try running your code again to see if the error has been resolved.

If the issue persists, please share more details about your environment, such as the Node.js version and the version of the OpenAI package you are using. This will help diagnose the problem more accurately.

1 Like

Still it’s giving the same error
I’m using 4.0.1 version of Node openai npm library
and nodejs version : 18.14.0

1 Like

Hello there.

I was having the same error and found out this is due to some changes in the npm openai version 4 package. Check the Migration Guide in the openai-node github repository including all significant changes from v3 to v4, those changes include the initialization of the api, creating transcriptions and more.

4 Likes

https://chat.openai.com/share/b175130a-0d77-465e-8187-59b92590df8b

I created a ChatGPT-4 chat that contains the migration guide text and is ready for a query, if you get code issues run them into that chat and you should get a useful result.

5 Likes

I got the same question here, it shows “const configuration = new OpenAIApi({
TypeError: OpenAIApi is not a constructor” after I change it into OpenAIApi

Could you help me in this case?

Here you go, champ…

1 Like

Thank You I’t really helps
but having another issue

  Problems loading reference 'https://json.schemastore.org/package': Unable to load schema from 'https://json.schemastore.org/package': connect ETIMEDOUT 20.42.128.105:443.

problem showing some fault in package.json but i checked it’s correct

ha, this was great!. it’s been 8 months since I used the javascript libraries… been all python. i forgot all these things…

Thanks for making this nice little chat script for me to update my older node scripts!

2 Likes

Thank you so much for the migration guide! Saved me a huge amount of time :slight_smile:

1 Like