ReferenceError: fetch is not defined in import OpenAI from 'openai'
Problem
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 - [X] This is an issue with the Node library 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 1 | import {Injectable} from "@nestjs/common"; 2 | import {ChatCompletionMessageParam} from "openai/resources/chat"; > 3 | import OpenAI from 'openai'; at Object.<anonymous> (../../../../node_modules/openai/_shims/fetch.js:8:17) at Object.<anonymous> (../../../../node_modules/openai/core.js:64:17) at Object.<anonymous> (../../../../node_modules/openai/src/index.ts:116:7) To Reproduce 1. Create a NestJS API 2. Install the openai library via npm 3. Create a controller that returns a response when a message is passed through via POST 4. Create a test suite in Jest to test this endpoint 5. Error occurs Code snippets _No response_ OS macOS Node version Node v18.17.1 Library version openai 4.6.0
Error Output
Error: fetch is not defined
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Node Fetch Polyfill for Jest Tests
The error 'ReferenceError: fetch is not defined' occurs because the 'fetch' API is not available in Node.js by default. The OpenAI library relies on 'fetch' for making HTTP requests, which is available in browser environments but not in Node.js. Jest tests run in a Node environment, leading to this issue when the OpenAI library is used.
Awaiting Verification
Be the first to verify this fix
- 1
Install Node Fetch Polyfill
To resolve the 'fetch is not defined' error, install the 'node-fetch' package, which provides a compatible 'fetch' implementation for Node.js.
bashnpm install node-fetch - 2
Import Node Fetch in Jest Setup
Create or update your Jest setup file to include the 'node-fetch' polyfill. This ensures that 'fetch' is available globally during your tests.
typescriptimport fetch from 'node-fetch'; global.fetch = fetch; - 3
Configure Jest to Use the Setup File
Ensure that your Jest configuration points to the setup file where 'fetch' is polyfilled. This can be done by adding a 'setupFilesAfterEnv' entry in your Jest configuration.
javascriptmodule.exports = { setupFilesAfterEnv: ['<rootDir>/jest.setup.js'] }; - 4
Run Your Jest Tests
After making the above changes, run your Jest tests again to verify that the 'fetch is not defined' error is resolved.
bashnpm test - 5
Verify OpenAI Library Functionality
Check that the OpenAI library functions correctly in your tests by asserting expected responses from your API endpoints.
typescriptexpect(response).toEqual(expectedResponse);
Validation
Confirm that the tests run successfully without the 'fetch is not defined' error. Additionally, validate that the OpenAI API calls return expected results in your Jest tests.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep