langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
11.77k stars 1.97k forks source link

Next.js build failing after version 14.2 with ConversationChain #5235

Open TheOrcDev opened 2 months ago

TheOrcDev commented 2 months ago

Checked other resources

Example Code

The followind code:

import { ChatOpenAI } from "@langchain/openai";
import { BufferMemory } from "langchain/memory";
import { ConversationChain } from "langchain/chains";

const model = new ChatOpenAI({
  modelName: process.env.GPT_MODEL,
  openAIApiKey: process.env.GPT_API_KEY,
  temperature: parseFloat(process.env.GPT_TEMPERATURE ?? "0.8"),
  maxTokens: parseInt(process.env.GPT_MAX_TOKENS ?? "1048"),
});

const memory = new BufferMemory();

const chain = new ConversationChain({
  llm: model,
  memory,
  verbose: true,
});

Error Message and Stack Trace (if applicable)

Error message on build only:

./node_modules/.pnpm/langchain@0.1.36/node_modules/langchain/dist/chains/analyze_documents_chain.js + 5 modules
Cannot get final name for export 'getEncoding' of ./node_modules/.pnpm/@langchain+core@0.1.61_openai@4.38.5/node_modules/@langchain/core/utils/tiktoken.js

./node_modules/.pnpm/langchain@0.1.36/node_modules/langchain/dist/memory/entity_memory.js + 3 modules
Cannot get final name for export 'Serializable' of ./node_modules/.pnpm/@langchain+core@0.1.61_openai@4.38.5/node_modules/@langchain/core/load/serializable.js

./node_modules/.pnpm/langchain@0.1.36/node_modules/langchain/dist/output_parsers/openai_functions.js + 1 modules
Cannot get final name for export 'compare' of ./node_modules/.pnpm/@langchain+core@0.1.61_openai@4.38.5/node_modules/@langchain/core/utils/json_patch.js

Description

Everything is working locally, only build is failing, so deployment is not possible.

System Info

"langchain": "^0.1.36",
"next": "^14.2.3",
dosubot[bot] commented 2 months ago

The error messages you're encountering during the Next.js build process seem to stem from issues related to ES module imports and possibly the handling of dependencies by Next.js. Here's a more focused approach to troubleshoot and potentially resolve these issues:

  1. Ensure Compatibility with ES Modules: Verify that your project setup and Next.js version are fully compatible with ES modules, as LangChain.js is likely utilizing them. Next.js generally supports ES modules out of the box, but custom configurations might interfere with this.

  2. Check Node Version: Make sure you're using Node.js version 18 or higher, as compatibility issues might arise with older versions.

  3. Review Import Paths: Align your import statements with the exports field in LangChain.js's package.json. Incorrect import paths can lead to the errors you're seeing. For instance, ensure you're using the correct path for importing Serializable and other modules.

  4. Install Necessary Dependencies: Go through the dependencies and peerDependencies listed in LangChain.js's package.json. While peerDependencies are optional, they might be required for your specific use case, especially for integrations with external services.

  5. Adjust TypeScript Configuration: If your project uses TypeScript, ensure your tsconfig.json is properly configured to handle ES modules and the library's type definitions.

  6. Examine Build Process: Consider if your build process is aligned with handling libraries structured as ES modules. The error might be related to how Next.js is processing these modules during the build.

  7. Install Relevant Peer Dependencies: Some functionalities might require installing peer dependencies listed in LangChain.js. Check if adding these dependencies resolves the issue.

If after trying these steps you're still facing build issues, it might be helpful to create a minimal reproduction case to isolate the problem further. Additionally, reviewing similar issues in the LangChain.js repository or related discussions might provide more context or solutions specific to your problem.

#### Details **Sources** - [Error : Cannot get final name for export 'getRuntime' of ./node_modules/openai/_shims/auto/runtime-node.mjs](https://github.com/langchain-ai/langchainjs/issues/5161) - [Getting [ERR_PACKAGE_PATH_NOT_EXPORTED] on execution](https://github.com/langchain-ai/langchainjs/issues/4269) - [Package.json import issues](https://github.com/langchain-ai/langchainjs/issues/3041) - [Cannot use `@langchain/community` due to broken `package.json`](https://github.com/langchain-ai/langchainjs/issues/4084) - [Issue importing submodules using es6 module import from documentation](https://github.com/langchain-ai/langchainjs/issues/45) - [langchain/package.json](https://github.com/langchain-ai/langchainjs/blob/main/langchain/package.json) - [libs/langchain-community/package.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/package.json) - [libs/langchain-community/tsconfig.cjs.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/tsconfig.cjs.json) - [package.json](https://github.com/langchain-ai/langchainjs/blob/main/package.json) - [langchain-core/package.json](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/package.json)
**About Dosu** 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.

To continue the conversation, mention @dosu.

hnykda commented 2 months ago

It's very similar to what I saw in the past with https://github.com/langchain-ai/langchainjs/issues/2335 .

We have production building fine, but now due to totally unrelated changes (really just some FE stuff and some DB comm) on a different feature branch, the build fails. The bot stuff are not helpful.

For us, it's the:

Failed to compile.

./node_modules/langchain/dist/output_parsers/openai_functions.js + 1 modules
Cannot get final name for export 'compare' of ./node_modules/@langchain/core/utils/json_patch.js

Anyway, I tried a similar workaround as in that other PR, adding an import to somewhere in our codebase, and it works 🤦 :

// hacks.ts, import somewhere in your codebase
import { compare } from '@langchain/core/utils/json_patch';
const DoNotRemove = compare;
TheOrcDev commented 2 months ago

It's very similar to what I saw in the past with #2335 .

We have production building fine, but now due to totally unrelated changes (really just some FE stuff and some DB comm) on a different feature branch, the build fails. The bot stuff are not helpful.

For us, it's the:

Failed to compile.

./node_modules/langchain/dist/output_parsers/openai_functions.js + 1 modules
Cannot get final name for export 'compare' of ./node_modules/@langchain/core/utils/json_patch.js

Anyway, I tried a similar fix as in that other PR, adding an import to somewhere in our codebase, and it works 🤦 :

// hacks.ts, import somewhere in your codebase
import { compare } from '@langchain/core/utils/json_patch';
const DoNotRemove = compare;

This weird eye hurting fix left me with two build problems :D

./node_modules/.pnpm/langchain@0.1.36/node_modules/langchain/dist/chains/analyze_documents_chain.js + 5 modules
Cannot get final name for export 'getEncoding' of ./node_modules/.pnpm/@langchain+core@0.1.61_openai@4.38.5/node_modules/@langchain/core/utils/tiktoken.js

./node_modules/.pnpm/langchain@0.1.36/node_modules/langchain/dist/memory/entity_memory.js + 3 modules
Cannot get final name for export 'Serializable' of ./node_modules/.pnpm/@langchain+core@0.1.61_openai@4.38.5/node_modules/@langchain/core/load/serializable.js
hnykda commented 2 months ago

You just have to hurt yourself a bit more by figuring out where from you could import those two getEncoding and Serializable and do the same shenanigas

TheOrcDev commented 2 months ago

You just have to hurt yourself a bit more by figuring out where from you could import those two getEncoding and Serializable and do the same shenanigas

Here is the fix, prepare yourself.

import { compare } from "@langchain/core/utils/json_patch";
import { Serializable } from "@langchain/core/load/serializable";
import { getEncoding } from "@langchain/core/utils/tiktoken";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";

const DoNotRemoveCompare = compare;
const DoNotRemovegetEncoding = getEncoding;
const DoNotRemoveSerializable = Serializable;
const DoNotRemoveRecursiveCharacterTextSplitter = RecursiveCharacterTextSplitter;

That's all I can do before the actual fix. Thanks @hnykda !

fsa317 commented 4 weeks ago

I'm also have this issue using next 14.2.* (if I downgrade to 14.1 it works)

jacoblee93 commented 3 weeks ago

Hi folks, apologies for missing this - will dig in as soon as I can.

jacoblee93 commented 3 weeks ago

It seems to work ok here (with 0.2.x):

https://github.com/langchain-ai/langchain-nextjs-template

I wonder if it's some shenanigans with multiple core versions? Could you check which version(s) are installed and try setting resolutions/overrides as shown here?

https://js.langchain.com/v0.2/docs/how_to/installation/#installing-integration-packages