Skip to main content
Question

Openai causes "Unexpected token ..." ...opts,

  • July 5, 2024
  • 3 replies
  • 294 views

Saad_Chaudhry1

I am new to JS. Im trying to implement an API call to openai, and using:
import OpenAI from “openai”.

tsconfig:
“compilerOptions”: {
“target”: “es6”,
“lib”: [
“es6”
],

Any help with setting the right target?

This topic has been closed for replies.

3 replies

Gleb
  • Power Member
  • 4716 replies
  • July 5, 2024

You can’t use ts compiler to add libraries, you need to use a bundler like esbuild, webpack or something like that and compile everything into a single file. Also you likely won’t be able to use a library like that on the plugin backend, so I recommend you do it on the plugin UI side.


Saad_Chaudhry1

Thanks Gleb. I’m using webpack (by following Libraries and Bundling), and so far been able to use papaparse npm module.

“dependencies”: {
@types/papaparse”: “^5.3.14”,
“openai”: “^4.49.0”,
“papaparse”: “^5.4.1”
}

I’ve now npm install openai the same way, but I get the “Unexpected token …” error because of spread syntax.

Is there any setting I should change in my tsconfig to be able to use openai libraries?


Gleb
  • Power Member
  • 4716 replies
  • July 6, 2024

Ah, ok. You can try es5 but es6 should work just fine as well.