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();
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();
Share
Improve this question
edited Aug 22, 2023 at 10:04
dev_light
3,9197 gold badges14 silver badges30 bronze badges
asked Aug 22, 2023 at 9:47
Rashmika SatishRashmika Satish
551 gold badge1 silver badge7 bronze badges
8 Answers
Reset to default 9import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
});
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.
import { OpenAI } from "openai";
const openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
create .env file and paste your api key generated from https://platform.openai.com/account/api-keys
if you want to genarate images the,
const response = openai.images.generate();
Editing the import of OpenAI API key and the constant function did the trick for me.
import { OpenAI } from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
It is a openai v4 change to the api hence this issue
import OpenAI from "openai";
This worked for me, if you are working on image generation using DallE (OpenAIv4)
for version 4
import { OpenAI } from "openai"; const openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
it makes a lot of sense why they did what they did. if you're trying to create JSM DALL-E application & your openai version is 4.
"openai": "^4.29.2"
then this might help you.
upgrade code to
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY,});
from
const config = new Configuration({apiKey:process.env.OPENAPI_KEY});
const openai = new OpenAIApi(config);
import OpenAI from "openai";