I was trying out openAI's api when I came across this error.
It was quite annoying. To solve it, you add the "type": "module"
to your package.json file.
If you don't have a package.json file, use npm init -y"
to create one.
With that, you should have this:
which should solve it.
but...something else might come up. Since you need your API key in a .env file to access the api, you would need to import the dotenv package.
after installing with npm or yarn, I used the require keyword to import it:
require('dotenv').config();
For some reason, that seems to throw another error:
It was very frustrating and stack overflow didn't seem to have a direct answer.
To solve it, change require to a normal import statement,
in this case it would be, import dotenv from 'dotenv/config'
the final code looks like this:
with that, you are done.
Top comments (0)