openai / openai-node

The official Node.js / Typescript library for the OpenAI API
https://www.npmjs.com/package/openai
Apache License 2.0
7.63k stars 814 forks source link

fetch is not exported from 'openai/_shims/fetch' #243

Closed YourAverageTechBro closed 1 year ago

YourAverageTechBro commented 1 year ago

Confirm this is a Node library issue and not an underlying OpenAI API issue

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
YourAverageTechBro commented 1 year ago

Found the fix for the issue.

I am building this app in NextJS and the endpoint needs to run on edge.

rattrayalex commented 1 year ago

Thanks @YourAverageTechBro ! Can you share more about what you did to fix this, in case it might help others in the future?

YourAverageTechBro commented 1 year ago

Yes of course.

I was running into the problem when creating a route handler via NextJS 13 App router.

The root cause of the issue was the fact that I was invoking the open ai endpoint from a serverless function and not an edge function.

To fix this I included export const runtime = "edge"; at the top of my file to indicate that this is an edge function, not a serverless function, and this resolved my issue.

rattrayalex commented 1 year ago

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?

YourAverageTechBro commented 1 year ago

Sure thing.

Here is my package.json.

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "postinstall": "prisma generate"
  },
  "dependencies": {
    "@clerk/nextjs": "^4.22.0",
    "@google-cloud/storage": "^6.12.0",
    "@headlessui/react": "^1.7.15",
    "@heroicons/react": "^2.0.18",
    "@pinecone-database/pinecone": "^0.1.6",
    "@prisma/client": "^5.0.0",
    "@types/node": "20.4.2",
    "@types/react": "18.2.15",
    "@types/react-dom": "18.2.7",
    "@types/uuid": "^9.0.2",
    "@upstash/qstash": "^0.3.6",
    "autoprefixer": "10.4.14",
    "chrome-types": "^0.1.219",
    "dotenv": "^16.3.1",
    "eslint": "8.45.0",
    "eslint-config-next": "13.4.10",
    "langchain": "^0.0.118",
    "next": "^13.4.13",
    "node-fetch": "^3.3.2",
    "openai": "^4.2.0",
    "pdf-parse": "^1.1.1",
    "postcss": "8.4.26",
    "prisma": "^5.0.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-pdf": "^7.3.3",
    "tailwindcss": "3.3.3",
    "typescript": "5.1.6",
    "uuid": "^9.0.0",
    "zod": "^3.21.4"
  },
  "devDependencies": {
    "encoding": "^0.1.13",
    "raw-loader": "^4.0.2"
  }
}
rattrayalex commented 1 year ago

Thanks! @jedwards1211 may have some followup questions.

frasergr commented 1 year ago

I encountered what looks like the same issue today as well.

For me, everything still seems to work but the errors show up during build of a nextjs app. I tried with node-fetch@3.3.2, the error went away and the route using OpenAI still worked but I didn't test anything more than that.

Logs:

./node_modules/.pnpm/node-fetch@2.6.11/node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in '/home/node_modules/.pnpm/node-fetch@2.6.11/node_modules/node-fetch/lib'

Import trace for requested module:
./node_modules/.pnpm/node-fetch@2.6.11/node_modules/node-fetch/lib/index.js
./node_modules/.pnpm/openai@4.2.0/node_modules/openai/_shims/fetch.node.mjs
./node_modules/.pnpm/openai@4.2.0/node_modules/openai/core.mjs
./node_modules/.pnpm/openai@4.2.0/node_modules/openai/index.mjs
./src/app/api/chat/route.ts
leolorenzoluis commented 1 year ago

@rattrayalex Getting the same error even if explicitly overriding fetch, so this is a library issue at this point. https://github.com/openai/openai-node/issues/202

Latest version of this library requires edge to be set for it to work in NextJS app.

jedwards1211 commented 1 year ago

@YourAverageTechBro I wonder if this has to do with your webpack config...can you paste your next.config.js? Or if you would be able to make a repo that reproduces the example it would be great

YourAverageTechBro commented 1 year ago

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.

T1LT commented 1 year ago

@YourAverageTechBro the issue persists for me even after adding the export const runtime = "edge" at the top of the file that has the OpenAI instantiation. I tried various fixes like installing node-fetch, moving the instantiation and request to the client side, and creating a separate route for it. Right now I'm trying to use it as a util function in a route that I defined previously. Would greatly appreciate any tips or the sample repo might be helpful too.

rattrayalex commented 1 year ago

@T1LT can you provide a https://stackblitz.com/ with a minimal repro of the problem you're seeing?

T1LT commented 1 year ago

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

joschan21 commented 1 year ago

Having this same issue right now (edit) I suspect this might have something to do with a react-pdf incompatability in the next.config.js

jedwards1211 commented 1 year ago

@T1LT can you share a screenshot of what error you see? I'm seeing TypeError: s.tee is not a function or its return value is not iterable but not OP's error.

@YourAverageTechBro /\.node/ definitely matches things it shouldn't like node_modules/openai/_shims/fetch.node.js. You meant to match files ending in .node right? If so you could use /\.node$/. We could use a different naming scheme though.

T1LT commented 1 year ago

@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

jedwards1211 commented 1 year ago

Weird, here's what I'm seeing:

image

If I click either of the buttons in the website I get errors in the Chrome console, but not in the Stackblitz console

jedwards1211 commented 1 year ago

@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

T1LT commented 1 year ago

@jedwards1211 that fixed it! thank you so much!

rattrayalex commented 1 year ago

@T1LT how did the .node regex find its way into your config? Was it a default value? Did you copy it from a blog post? Did everyone here independently come up with it themselves?

T1LT commented 1 year ago

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

rattrayalex commented 1 year ago

This is fixed in https://github.com/openai/openai-node/issues/276 which will be out in the next release! In the meantime, use the workaround documented here.

dilizarov commented 1 year ago

This is still broken after the 4.5.0 release @rattrayalex. I'm using the NestJS framework (so this is all happening in Node.js) for my backend and had to migrate the OpenAI library from 3.x to 4.x to fix a bug, but then when I pushed my changes to Heroku, this is the error I get:

[heroku-exec] Starting
Sep 06 13:59:16 [app/web.1] > server@0.0.1 start:prod /app
Sep 06 13:59:16 [app/web.1] > node -r esm dist/main
Sep 06 13:59:19 [heroku/web.1] State changed from starting to crashed
Sep 06 13:59:19 [app/web.1] /app/node_modules/openai/_shims/fetch.js:1
Sep 06 13:59:19 [app/web.1] ReferenceError: fetch is not defined
Sep 06 13:59:19 [app/web.1]     at Object.<anonymous> (/app/node_modules/openai/_shims/fetch.js:8:17)
Sep 06 13:59:19 [app/web.1]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
Sep 06 13:59:19 [app/web.1]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
Sep 06 13:59:19 [app/web.1] npm ERR! code ELIFECYCLE
Sep 06 13:59:19 [app/web.1] npm ERR! errno 1
Sep 06 13:59:19 [app/web.1] npm ERR! server@0.0.1 start:prod: `node -r esm dist/main`
Sep 06 13:59:19 [app/web.1] npm ERR! Exit status 1

I can confirm the library version is 4.5.0 when this happens.

For more system context:

Node version: 14.17.6 NPM version: 6.14.15 Heroku Stack: heroku-20

rattrayalex commented 1 year ago

@dilizarov can you share your package.json? If you're using TypeScript, can you share your tsconfig, and if you're using something else to compile your JS, can you share that config?

dilizarov commented 1 year ago

@rattrayalex sure.

package.json:

{
  "name": "server",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "engines": {
    "node": "14.17.x"
  },
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start -e \"node -r esm\"",
    "start:dev": "nest start --watch -e \"node -r esm\"",
    "start:debug": "nest start --debug --watch -e \"node -r esm\"",
    "start:prod": "node -r esm dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@mailerlite/mailerlite-nodejs": "^1.1.0",
    "@nestjs/common": "^8.4.5",
    "@nestjs/core": "^8.4.5",
    "@nestjs/passport": "^8.2.1",
    "@nestjs/platform-express": "^8.4.5",
    "@nestjs/schedule": "^2.0.1",
    "@prisma/client": "^4.4.0",
    "@tfarras/nestjs-firebase-admin": "^2.0.1",
    "@tfarras/nestjs-firebase-auth": "^2.0.0",
    "@types/cookie-parser": "^1.4.3",
    "@types/cron": "^2.0.0",
    "@types/express": "^4.17.8",
    "@types/morgan": "^1.9.3",
    "@types/number-to-words": "^1.2.1",
    "@types/passport-anonymous": "^1.0.3",
    "@types/react-redux": "^7.1.24",
    "@types/sharp": "^0.30.2",
    "@types/uuid": "^8.3.4",
    "aws-sdk": "^2.1143.0",
    "axios": "^0.27.2",
    "chalk": "^4.1.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "cookie-parser": "^1.4.6",
    "dotenv": "^16.0.1",
    "dotenv-flow": "^3.2.0",
    "esm": "^3.2.25",
    "firebase-admin": "^10.2.0",
    "ioredis": "^5.3.1",
    "lodash": "^4.17.21",
    "mailparser": "^3.5.0",
    "method-override": "^3.0.0",
    "moment": "^2.29.4",
    "morgan": "^1.10.0",
    "nanoid": "^3.3.4",
    "nest-commander": "^2.5.0",
    "number-to-words": "^1.2.4",
    "oauth-1.0a": "^2.2.6",
    "openai": "^4.5.0",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.0",
    "plaid": "^10.4.0",
    "postmark": "^3.0.11",
    "prisma": "^4.4.0",
    "query-string": "^7.1.1",
    "react-redux": "^8.0.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.5.5",
    "sharp": "^0.30.5",
    "slugify": "^1.6.5",
    "stream-chat": "^6.5.1",
    "stripe": "^9.4.0",
    "twilio": "^3.77.1",
    "type-fest": "^3.0.0",
    "uuid": "^8.3.2",
    "ws": "^8.6.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.2.6",
    "@nestjs/schematics": "^8.0.11",
    "@nestjs/testing": "^8.4.5",
    "@types/dotenv-flow": "^3.2.0",
    "@types/jest": "27.5.1",
    "@types/lodash": "^4.14.182",
    "@types/multer": "^1.4.7",
    "@types/node": "^17.0.35",
    "@types/passport-jwt": "^3.0.6",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.26.0",
    "@typescript-eslint/parser": "^5.26.0",
    "eslint": "^8.16.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.0",
    "prettier": "^2.6.2",
    "supertest": "^6.2.3",
    "ts-jest": "28.0.3",
    "ts-loader": "^9.3.0",
    "ts-node": "10.8.0",
    "tsconfig-paths": "^4.0.0",
    "typescript": "^4.7.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "resolveJsonModule": true,
    "paths": {
      "*": ["node_modules/*", "src/*"]
    },
    "typeRoots": ["types", "./node_modules/@types"]
  }
}

tsconfig.build.json:

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
rattrayalex commented 1 year ago

Thanks @dilizarov . And you can confirm you had the same issue in v4.4.0?

dilizarov commented 1 year ago

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

rattrayalex commented 1 year ago

@dilizarov interesting – would you be willing to share a repo that contains a minimal reproduction of this problem, with steps on how to trigger it?

EDIT: actually, I think what you're experiencing is actually a distinct issue @dilizarov . Would you mind opening a separate issue, ideally with the reproduction or details from above?

rattrayalex commented 1 year ago

I believe the issue @dilizarov reported has now been opened as #304