Stuck on the PollyGlot Project b/c I can't deploy locally

Hi all,
I cannot for my life find a way to deploy a simple AI chat with OpenAI in the way described in the PollyGlot project.

I can run the project within Scrimba no problem but when I try to deploy to my local machine I cannot get past a series of console errors that insist the import/configuration is at fault but I cannot find a solution.

Basically if I use something like

import { Configuration, OpenAIApi } from "openai";

// Create configuration using your API key
const configuration = new Configuration({
  apiKey: import.meta.env.VITE_OPENAI_API_KEY,
});

// Create an instance of the OpenAIApi client
const openai = new OpenAIApi(configuration);

I get told to import via a more simple method like, import OpenAI from, so I use this -

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: import.meta.env.VITE_OPENAI_API_KEY,
});

After which I’m told there is an error with the import and should use a configuration const.

Anyone have any experience with resolving something like this?

1 Like

Hey @treadly,

Could you check if you’ve installed the latest openai package? I had an similar issue but just did a npm install openai@latest. Not sure if it’s the same problem but always good to check first.

Hi @roku , thanks for the response. I have tried the @lastest for the openai package but still stuck.

For reference, this is the error in the console I get when using the basic import OpenAI from “openai”

  • Uncaught TypeError: Failed to resolve module specifier “openai”. Relative references must start with either “/”, “./”, or “…/”.

The suggested change to resolve this is to implement what I mentioned in the first code snippet. Implementing one or the other leads to an error in which the suggestion is to use the other method.

I would really love to complete this course but this roundabout problem is crushing me

1 Like

Hey @treadly sorry for the delay. Had a session to try and figure our what this issue could be and I remembered that changing my package.json to specify it as:

{
  "type": "module"
}

though I don’t think this is the solution, maybe? Might have just been coincidence but you can try that if you don’t have that already.

Could you be able to supply your entire codebase on here, plus your package.json file? Can give us a better idea of the context. Just remember to omit your API keys.