Search code examples
javascriptreactjsexpressmernopenai-api

The requested module 'openai' does not provide an export named 'Configuration' error


I'm trying to build an AI image generating website using MERN and I got this error:

The requested module 'openai' does not provide an export named 'Configuration'.

file:///C:/Users/Rashmika%20Satish/ai_website/server/routes/dalleRoutes.js:3 import {Configuration, OpenAIApi} from 'openai'; ^^^^^^^^^^^^^ SyntaxError: The requested module 'openai' does not provide an export named 'Configuration' at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21) at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.15.0 [nodemon] app crashed - waiting for file changes before starting...

this is the dalleRoutes.js page:

import express from 'express';
import * as dotenv from 'dotenv';
import {Configuration, OpenAIApi} from 'openai';



dotenv.config();

const router = express.Router();

This is the index.js page:

import express from 'express'
import *  as dotenv from 'dotenv';
import cors from 'cors';

import connectDB from './mongodb/connect.js';

import postRoutes from './routes/postRoutes.js';
import dalleRoutes from './routes/dalleRoutes.js';

dotenv.config();

const app = express();
app.use(cors());
app.use(express.json({limit: '50mb'}));

app.use('/api/v1/post', postRoutes);
app.use('/api/v1/dalle', dalleRoutes);

app.get('/', async(req, res)=>{
    res.send('Hello from CreateAI');
})

const startServer = async () =>{


    try{
        connectDB(process.env.MONGODB_URL);
        app.listen(8080, () => console.log('Server has started on port http://localhost:8080'))
    }catch(error){
         console.log(error);
    }
    

}
startServer();

This is the postRoutes.js page

import express from 'express';
import * as dotenv from 'dotenv';
import {v2 as cloudinary} from 'cloudinary';

import Post from '../mongodb/models/post.js';

dotenv.config();

const router = express.Router();

Solution

  • I've got this same error. I'm assuming that you're following the JSM Tutorial to create the app. After a lot of searching, I finally found a similar discussion on the OpenAI Forum 5 days back, and it seems like it's a version change - configuring the api key has been simplified in v4.

    Follow the Forum here, if you're interested: https://community.openai.com/t/getting-an-error-when-importing-configuration-and-openaiapi-from-openai/325012

    Here's the Github Guide on Migrating from v3 to v4: https://github.com/openai/openai-node/discussions/217

    In Short, just run npm exec openai migrate and it should automatically migrate and change the code in your codebase to the latest version and should fix this version issue.