Skip to content
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

OpenAI import fails in Vercel deployment Cannot find module '/var/task/node_modules/openai/_shims/agent-node.mjs' #2558

Closed
raphaelmsr opened this issue Sep 7, 2023 · 8 comments
Assignees
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@raphaelmsr
Copy link

Using NextJS 13 without appDir, I just installed LangchainJS for use in my tRPC back-end
In local everything works like a charm.

After deploying on Vercel as a Serverless function, I have this strange error

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/openai/_shims/agent-node.mjs' imported from /var/task/node_modules/openai/core.mjs at new NodeError (node:internal/errors:405:5) at finalizeResolution (node:internal/modules/esm/resolve:329:11) at moduleResolve (node:internal/modules/esm/resolve:992:10) at moduleResolveWithNodePath (node:internal/modules/esm/resolve:936:12) at defaultResolve (node:internal/modules/esm/resolve:1178:79) at nextResolve (node:internal/modules/esm/loader:163:28) at ESMLoader.resolve (node:internal/modules/esm/loader:835:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40) at link (node:internal/modules/esm/module_job:76:36) { code: 'ERR_MODULE_NOT_FOUND' }

I couldn't find any issue mentioning this error anywhere, hope this is the right place to open an issue !
Thanks in advance

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Sep 7, 2023
@dosubot
Copy link

dosubot bot commented Sep 7, 2023

🤖

Hello,

The error you're encountering is likely due to the fact that Vercel's serverless functions do not fully support ESM (ECMAScript Modules) yet. The .mjs file extension is used for ESM in Node.js. The openai package, which is a dependency of LangChain, uses ESM and thus the error occurs when deploying to Vercel.

To resolve this issue, you can try to bundle your serverless function with a tool like Webpack or esbuild, which will convert ESM imports to CommonJS, which is supported by Vercel.

Here is an example of how you can use esbuild to bundle your serverless function:

  1. Install esbuild as a dev dependency:
npm install --save-dev esbuild
  1. Add a build script to your package.json:
"scripts": {
  "build": "esbuild src/pages/api/hello-serverless.ts --bundle --platform=node --target=node14 --outfile=dist/hello-serverless.js"
}
  1. Run the build script:
npm run build
  1. Update your Vercel configuration to use the bundled file:
{
  "functions": {
    "api/hello-serverless.js": {
      "runtime": "nodejs14.x",
      "includeFiles": "dist/**"
    }
  }
}
  1. Deploy your application:
vercel --prod

This will bundle your serverless function into a single file that uses CommonJS, which should resolve the error.

As for LangChain specific configurations or requirements for deployment on serverless platforms like Vercel, I couldn't find any specific information in the repository. However, the general rule of thumb is to ensure that all dependencies are correctly installed and that the environment supports the features used by LangChain, such as ESM.

I hope this helps! Let me know if you have any other questions.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@jacoblee93
Copy link
Collaborator

Hey @raphaelmsr, we recently upgraded to OpenAI's v4 SDK under the hood - does downgrading to 0.0.140 fix it?

https://github.com/hwchase17/langchainjs/releases/tag/0.0.140

Also, in the short term, things should work in Next.js apps WITH the app directory if that's an option for you:

https://github.com/langchain-ai/langchain-nextjs-template/blob/main/package.json

@raphaelmsr
Copy link
Author

Hey @jacoblee93 ! thanks for the quick reply; will try downgrading !
I am using the T3 stack framework which is not compatible with appDir for now, but looking forward to it tho 😊

EDIT : IT WORKED 😱 Can't believe I spent the day on this haha Thanks a million for such a quick reply
I have a few questions

  1. what do I miss with OpenAI v4's SDK ?
  2. what do I miss with Langchain > 0.0.140
  3. What's happening 😭 What is this bug even about
    Thanks again!!

@jacoblee93
Copy link
Collaborator

jacoblee93 commented Sep 7, 2023

Glad to hear it! Sorry you're encountering this.

  1. The transition to OpenAI v4 should have been invisible, so you're not missing any additional functionality there
  2. A few nice features - I'll open an issue with OpenAI to try to get to the root of why this is happening so that you can upgrade!
  3. Will investigate and report back but it appears to be from here:

https://github.com/openai/openai-node/blob/331eb4e27615cc83a23596f2b74ec2721453938f/src/_shims/agent-node.ts#L4

@jacoblee93
Copy link
Collaborator

Opened an issue on OpenAI as I was able to repro with their SDK alone. Will leave this open until it's patched there.

Thanks again for the report!

@raphaelmsr
Copy link
Author

Thanks for following up and opening tickets on the OpenAI repo! Will follow so I can finally update 😊

@jacoblee93
Copy link
Collaborator

@raphaelmsr I downgraded the OpenAI version in 0.0.146 so you should be able to upgrade to that.

Closing this for now but reopen if you encounter trouble again.

@raphaelmsr
Copy link
Author

Thanks for the update !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants