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

Most upvoted comments

@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:

Thanks @dilizarov https://github.com/dilizarov . And you can confirm you had the same issue in v4.4.0?

— Reply to this email directly, view it on GitHub https://github.com/openai/openai-node/issues/243#issuecomment-1710912572, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA5BT2SOFJNJLCVMQEWWCKDXZJPUXANCNFSM6AAAAAA34EMWE4 . You are receiving this because you were mentioned.Message ID: @.***>

@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)

fetch is not exported error fetch is not exported error 2

@rattrayalex here you go the error shows up when I try to hit submit.

Here is my next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config, options) => {
    // Important: return the modified config
    config.module.rules.push({
      test: /\.node/,
      use: "raw-loader",
    });
    return config;
  },
  experimental: {
    serverActions: true,
  },
};

module.exports = nextConfig;

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?