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

Errors when loading @langchain/core/messages in a typescript ES module #4645

Closed
5 tasks done
marc-weber1 opened this issue Mar 6, 2024 · 7 comments
Closed
5 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@marc-weber1
Copy link

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { BaseMessage, ChatMessage } from '@langchain/core/messages';

tsconfig:

{
	"compilerOptions": {
		"module": "Node16",
		"target": "ES2022",
		"outDir": "out",
		"lib": [
			"ES2022",
			"dom"
		],
		"sourceMap": true,
		"rootDir": "src",
		"strict": true
	}
}

Error Message and Stack Trace (if applicable)

@langchain/core/messages.d.cts:
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("./dist/messages/index.js")' call instead.

Description

Trying to make a VSCode extension, but can't with the required settings and langchain's cjs dependencies.

System Info

langchain@0.1.25 | MIT | deps: 18 | versions: 255
Typescript bindings for langchain
https://github.com/langchain-ai/langchainjs/tree/main/langchain/

keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores

dist
.tarball: https://registry.npmjs.org/langchain/-/langchain-0.1.25.tgz
.shasum: afd0faf2eadfb5e6e1d57bb3bbc5731918e62135
.integrity: sha512-sfEChvr4H2CklHdSByNBbytwBrFhgtA5kPOnwcBrxuXGg1iOaTzhVxQA0QcNcQucI3hZrsNbZjxGp+Can1ooZQ==
.unpackedSize: 4.0 MB

dependencies:
@anthropic-ai/sdk: ^0.9.1 js-tiktoken: ^1.0.7 openapi-types: ^12.1.3
@langchain/community: ~0.0.33 js-yaml: ^4.1.0 p-retry: 4
@langchain/core: ~0.1.36 jsonpointer: ^5.0.1 uuid: ^9.0.0
@langchain/openai: ~0.0.14 langchainhub: ~0.0.8 yaml: ^2.2.1
binary-extensions: ^2.2.0 langsmith: ~0.1.7 zod-to-json-schema: ^3.22.3
expr-eval: ^2.0.2 ml-distance: ^4.0.0 zod: ^3.22.4

maintainers:

dist-tags:
latest: 0.1.25 next: 0.1.19-rc.2

published 5 days ago by basproul braceasproul@gmail.com

Windows 10 Pro
npm v10.2.4
NodeJS v20.11.1

Copy link

dosubot bot commented Mar 6, 2024

Hey @marc-weber1! Long time no see in the digital corridors of GitHub issues. 🚀

Based on the information you've provided, it seems like you're encountering an issue with importing the '@langchain/core/messages' module in a TypeScript ES module while developing a VSCode extension with LangChain.js version 0.1.25. This issue could be related to the module and moduleResolution settings in your tsconfig.json file.

Given the context of your tsconfig.json, you have the module option set to "Node16" and the target option set to "ES2022". These settings are generally appropriate for working with ES modules in a Node.js environment. However, the error you're experiencing might stem from one of the following reasons:

  1. ES Module Compatibility: If '@langchain/core/messages' or any of its dependencies are not fully compatible with ES module syntax or the specific module target you've set ("ES2022"), this could lead to import errors. This is especially relevant if the package or parts of it are expecting a CommonJS module system.

  2. Node's Module Resolution: The "Node16" value for module is designed to align with Node.js's native ESM support, including subpath patterns and package exports. If '@langchain/core/messages' or its dependencies are not properly configured to support this resolution strategy, it could result in loading errors.

To troubleshoot and potentially resolve this issue, consider the following steps:

  • Check Package Exports: Ensure that '@langchain/core/messages' and its dependencies are correctly exporting modules for ES2022. Look for the package.json files in these packages to see if they have an "exports" field that supports the module format you're using.

  • Module Type Compatibility: Verify whether '@langchain/core/messages' is expected to be imported as an ES module or if it requires CommonJS-style imports. This can sometimes be determined by the documentation or by inspecting the package's package.json for clues such as the "type" field being set to "module" (for ES modules) or the presence of "main" and "exports" fields.

  • Adjust tsconfig Settings: As a test, you might try changing the module and moduleResolution settings in your tsconfig.json to see if a different configuration resolves the issue. For instance, setting module to "CommonJS" temporarily could help determine if the issue is related to ES module compatibility.

  • Dependencies and Node Version: Ensure that all dependencies are up to date and that you're using a version of Node.js that fully supports the features and module resolution strategy you're employing.

Without specific error messages or more detailed information about how you're attempting to import '@langchain/core/messages', these suggestions are based on common issues related to module resolution and compatibility in TypeScript projects targeting ES modules in a Node.js environment.

Regarding similar issues in the langchainjs repository, there are several open and closed issues that seem to be related to your problem. The most relevant one is Unable to import anything, which is still open and was last updated on March 02, 2024. The suggested solutions in this issue include reinstalling langchainjs, checking the LangChain version, and updating the next.config.js. However, these solutions have received mixed reactions and it appears that a definitive solution has not been reached in the discussion.

I hope this information helps you in resolving the issue. If you need further assistance, please provide more details about the error messages you're receiving and how you're attempting to import '@langchain/core/messages'.

Sources

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.

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

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Mar 6, 2024
@jacoblee93
Copy link
Collaborator

Hey @marc-weber1, can you share your package.json file too?

@marc-weber1
Copy link
Author

https://github.com/marc-weber1/langchain-vscode/blob/master/package.json

entire source code is on github, master branch as is should reproduce the error, may need vscode installed

@jacoblee93
Copy link
Collaborator

Sat"type": "module" in package JSON or use require

https://blog.logrocket.com/commonjs-vs-es-modules-node-js/

@marc-weber1
Copy link
Author

BaseMessage and ChatMessage are types, and I can't convert a VSCode extension to an ES6 module; How am I supposed to import types with a require statement?

@marc-weber1
Copy link
Author

microsoft/vscode#201935 - looks like they will fix this soon, but maybe include some documentation about either CommonJS not being supported or how to use it

@jacoblee93
Copy link
Collaborator

jacoblee93 commented Mar 10, 2024

Oh hmm interesting. How do people usually require types in CJS? Haven't worked with it in a long time.

Would something like this work?

const { ChatPromptTemplate, LengthBasedExampleSelectorInput } = await import("@langchain/core/prompts");

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