I’m stuck trying to use the OpenAI API in my Node.js project. When I run my code, I get a Cannot find module '@openai/api'
error. It’s really frustrating!
Here’s what I’ve tried so far:
- Installing the package with
npm install @openai/api
(got a 404 error) - Updating Node.js to version 19.1.0
- Creating a test script to check if it’s just my main project or a general issue
My test script looks like this:
const openai = require('openai');
openai.apiKey = 'my-api-key';
async function testOpenAI() {
try {
const response = await openai.Completion.create({
engine: 'text-davinci-002',
prompt: 'What\'s the weather like today?',
max_tokens: 30
});
console.log(response.choices[0].text);
} catch (error) {
console.error(error);
}
}
testOpenAI();
But when I run it, I still get the same error. The weird thing is, I can use the API through other tools like Postman without any issues.
Does anyone know what might be causing this problem or have any ideas on how to fix it? I’m totally lost here. Thanks for any help!