Having trouble importing openai api

I started a little project to make a small site. Tried to use the openai api with it but can’t import it. I have 2 script files, one is script.js which has the main logic for the game and another is bonuscards.js which has the logic for the openai. And this is where I import the main script file and the other one.

<script type="module" src="script.js"></script>
import { bonusCards, addBonusCard } from './bonuscards.js';
import OpenAI from "openai";

  "name": "javascript-card-game",
  "version": "1.0.0",
  "main": "bonuscards.js",
  "type": "module",
  "scripts": {
    "dev": "node bonuscards.js"
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "openai": "^4.4.0"

I have it installed in my dependencies, installed it locally and it works if I only run that file in the terminal. But it gives an error when I try to run it on vscode live server.
I looked for a solution for days but can’t find one. I’m trying to make a small game and if I finish with it I would like to publish it online so I need it to run on a server.
I’m looking for advises either for a fix to make it work with live server or if I’m doing something wrong entirely I would like do get an advice how should I run it and install the package to be able to publish it only once I’m done.

I hope I can find a solution for this and don’t have to stop this project. Thank you everyone.

you should use something like ‘create-react-app’ or whatever other app starter setup you can find., to get a stable environment up and running first doing all the VSCode stuff you need.

Only after that works then just add openai to that existing project.

Make sure you are using python3 to run it or it will not work.

openapi.yaml or aiplugin.json?

check the format and tell gpt 3.5 (hint)
it needs operation ids.

never steered me wrong.

that said i refer to it as a “new open api spec”

Do you think it would be good if I used an apache server? Because I would have to rewrite the whole code for react and I’m not really familiar with it either.

I’m using javascript. Do I need python as well?

I just meant get a “starter web project” of whatever kind you want, with whatever tech stack you want, and then add openai into that already working project. The problem you are having is a general project setup problem, and not an OpenAI problem.

Seconding @wclayf, this is not an OpenAI issue but an issue with your node project setup. If your goal is to have this be called by your game, you’re going to need some sort of server (apache/nginx/node http). Pick whatever you are most comfortable with, the OpenAI API’s are incredibly easy to write in any language. If you aren’t comfortable with any server tech, I’d suggest looking into Firebase Cloud Functions. Googling “firebase function openai” will get you a bunch of tutorials and you’ll have a working server up and running. Once you’ve got it running, don’t forget to properly secure your API so others can’t use it.

1 Like

Okey I understand, I will setup an apache server for it and run it on that. Just wanted to make sure I won’t run into another week long debugging.
Thanks for the answers, it helped a lot.