New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected token 'export' from formdata-node when using openai/shims/node #548
Comments
Updating to v3.3.0 got me past the issues I was having. I'll leave this open in case someone wants to look into why those issues were experienced with v4.20.0, but for those stuck where I was, v3.3.0 will likely be what you need |
Can you share tsconfig, package.json, and jest config? If you are using Note that v3.3.0 has many downsides, like no auto-retry, outdated/incorrect types, no support for streaming, etc. |
I did use Here's the tsconfig.json file:
And here's package.json which includes the jest config (I removed scripts and other private info)
|
Ah I'm so sorry, I meant |
To be clear, this was being used in a backend nodeJS environment. Would I still want to use the web shim? |
Yes, you can as long as you have access to the global |
I also encountered the same problem with:
I managed to get the tests working by first removing the line
//jest.setup.js
const { TextDecoder, TextEncoder, ReadableStream } = require("node:util")
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
})
const { Blob, File } = require("node:buffer")
const { fetch, Headers, FormData, Request, Response } = require("undici")
Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
})
//jest.config.js
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js'],
...other config
} |
Right on @RobertHaslinger, nailed it. I just needed Request and Response in my case, but great fix. |
Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
Issues experienced while running unit tests in our nodejs backend.
I first encountered the fetch is not defined bug, which led me to #304 and was able to move past the issue with
import 'openai/shims/node'
But now I'm receiving the following errors:
I've also added
But I'll still get the same
SyntaxError: Unexpected token 'export'
error.I've updated the
transformIgnorePatterns
value to include"/node_modules/(?!formdata-node)"
as well as"/node_modules/(?!openai/node/shims)"
and many combinations. Still no luck.yarn v1.22.4
typescript v5.1.6
jest v29.7.0
To Reproduce
yarn add openai
I've been able to reproduce it with as little as just the imports.
Add the following imports to a file that has unit tests (or a logic/utils file used by a file being tested)
run the tests on that file
Code snippets
No response
OS
macOS
Node version
Node v16.20.0 (also tested with v18.19.0 same results)
Library version
openai v4.20.0
The text was updated successfully, but these errors were encountered: