openai-node: fetch is not exported from 'openai/_shims/fetch'
Confirm this is a Node library issue and not an underlying OpenAI API issue
- This is an issue with the Node library
Describe the bug
I am getting the following error when trying to instantiate my OpenAI client.
- error ./node_modules/openai/core.mjs
Attempted import error: 'fetch' is not exported from 'openai/_shims/fetch' (imported as 'fetch').
To Reproduce
Instantiate openAI with the following:
import OpenAI from "openai";
export const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
Code snippets
The OpenAI definition in my package.json is "openai": "^4.2.0"
My tsconfig.json
file:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
### OS
macOS
### Node version
v20.2.0
### Library version
4.2.0
About this issue
- Original URL
- State: closed
- Created 10 months ago
- Reactions: 2
- Comments: 29
@rattrayalex the project I’m working on works with rendering and parsing PDFs. the library I used for rendering the PDFs (react-pdf) wasn’t working with the default config. I looked up a fix for it (can’t find the link), and they had me do this.
Yep!
On Thu, Sep 7, 2023 at 7:16 PM Alex Rattray @.***> wrote:
@T1LT if you have time, let me know if changing the webpack rule to
/\.node$/
makes the problem go away for you. I’m planning to rename our shim files to*-node.*
instead of*.node.*
to avoid this risk@jedwards1211 sure, here you go: (the stackblitz link might be buggy, sorry about that)
@rattrayalex here you go the error shows up when I try to hit submit.
Here is my
next.config.js
Let me know if that is enough information. I might be able to make a sample repro in a few days if you still need it to debug.
Got it, thanks @YourAverageTechBro! I think we’d want our library to work on serverless or edge runtimes seamlessly, so I think I’d regard this as a bug (unless there’s something particularly strange about your setup).
Could you share your
package.json
to help us with reproduction and debugging?