-
Notifications
You must be signed in to change notification settings - Fork 919
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
ReferenceError: fetch is not defined in import OpenAI from 'openai' #304
Comments
Thanks for reporting @JackLazenbyZigzag . Can you share your jest config, tsconfig, and package.json? |
No problem at all @rattrayalex! We use Nx to manage our projects via a monorepo setup so I will share the jest and ts configs for the particular project that is failing. Here they are: package.json:
jest.config.js:
tsconfig:
Thanks! |
Thanks! It looks like your jest and tsconfig extend base configs, would you mind sharing those as well? |
(I believe this may be the same bug mentioned here: #243 (comment)) |
@rattrayalex of course! As an FYI, I have obfsucated some things that are business specific to our project, in case it looks a bit empty! :-) We also only extend from a base tsconfig, the jest side of things just uses the rules out of the box that jest suggests. base tsconfig:
|
great, thanks @JackLazenbyZigzag ! We'll try to take a look at this on Monday. |
@rattrayalex I had a look at that prior to creating this and it does look like it could be similar! Thank you for your help, appreciate it. Have a great rest of your weekend! |
@JackLazenbyZigzag what jest We weren't able to reproduce the issue when using the We were able to get chat completion requests working with the |
Okay we were able to get even file uploads to work in the
in that specific order. |
Hi @jedwards1211 - thanks for this! I've added the import for cross-fetch/polyfill, and that resolved it for our existing project test suite (so not adding anything new test-wise). However, when trying to test our service specifically that uses the library, it still doesn't like it. I'm getting the below from the import for formdata-polyfill:
and, if I ignore this and try to test, I get this:
This only happens when I test a file that is directly using the openai library though. If I don't do that, the test suite now runs correctly. |
@JackLazenbyZigzag would you be able to use |
@jedwards1211 at this point I'm not trying to do any file related things. I've just built a very basic test suite for a basic service.ts file that handles the openai library interaction, like so: service ts file:
test ts file:
And this gives me the error:
|
Huh, in whatever version of the jest jsdom environment I have, Btw you only need one of |
@jedwards1211 I have removed whatwg-fetch for now, and removed the test file. I can re-add it once a fix or workaround has been identified :-) |
@JackLazenbyZigzag did you install Have you tried using the |
@rattrayalex yes, I have installed Also, just re-checked our jest.config for this test suite and it was always using node. Apologies for the mix up! If I change it to jsdom, then it does pass. Obviously this isn't ideal as it could increase test run speed, and we're running this in a backend environment which shouldn't require jsdom for our needs. |
If you can create a repository that reproduces the issue it would be the most helpful, otherwise, to make sure we're on the same page, you're able to reproduce the
Can you also share your |
@jedwards1211 I can get a test repo put together in the next day or so if needed, but in the meantime, in response to your questions regarding environment. In our case:
Jest.preset is pretty small, but here it is:
jest.config.js for this library:
|
Okay I was able to repro on Jest 27, I was testing on Jest 29 and didn't notice that was different from your project until now. I think support for package.json |
@dilizarov @JackLazenbyZigzag are you able to upgrade to Jest 29? If so, can you confirm the issue does not appear on that version? |
@rattrayalex we have a lot of jest dependencies in our project that mean we can't upgrade to the latest version of Jest from 27 without a lot of extra work, which I can't quickly do to investigate this I'm afraid. I will see what I can do over the next few days, though. |
Ah ok, don't worry about it then. We hope to have a workaround out within a day or two. |
We got the same problem here in AWS Lambda NodeJS 16 runtime. |
@ciryon can you share a minimal repo that reproduces the issue you're seeing? (Note that Node 16 is now EOL and we'll be dropping support for it soon) |
@rattrayalex not easily made into a minimal repo I'm afraid. The issue appeared when upgrading from version 3 of the library, and I've tried to rollback from latest all the way to 4.0.1 and the same problem still appears. |
Gotcha. And you only experience the problem on Lambda, not locally? |
Yes, it works locally! |
I can also report that it works if I use the Node 18 runtime on AWS Lambda. |
Ah, great. Thanks Christian! Glad you've found something that works – we'll be dropping support Node 16 support in the next release. |
For me updating all jest libraries to the latest versions helped. |
Hello there! There seems to have been a few issues around this that have been resolved recently, but I'm still getting it so thought I would share just in case it's something different.
Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
When building and running the OpenAI library in our NestJS API, we can do so locally with no issue ; we can communicate with it and get responses etc. However, we have a Jest testing suite, and when trying to test, it fails with:
ReferenceError: fetch is not defined
at Object. (../../../../node_modules/openai/_shims/fetch.js:8:17)
at Object. (../../../../node_modules/openai/core.js:64:17)
at Object. (../../../../node_modules/openai/src/index.ts:116:7)
To Reproduce
Code snippets
No response
OS
macOS
Node version
Node v18.17.1
Library version
openai 4.6.0
The text was updated successfully, but these errors were encountered: