-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
when developing with TypeScript and the compilation target is set to CommonJS, type error occurs. #3589
Comments
🤖 Hello @JerrysShan! 👋 I'm Dosu, a friendly bot here to assist you while we wait for a human maintainer. I can help you tackle bugs, answer questions, and guide you on your journey to become a contributor. You're in good circuits, so let's get to solving that TypeScript issue of yours! Based on the error message you provided, it seems you're trying to import an ECMAScript module in a CommonJS module. This is not allowed because ECMAScript modules use To resolve this issue, you can use dynamic imports which are supported in both CommonJS and ECMAScript modules. Here's an example of how you can modify your code: export const getExamples = async (query: string, apiKey: string) => {
const { MemoryVectorStore } = await import("langchain/vectorstores/memory");
const { OpenAIEmbeddings } = await import("langchain/embeddings/openai");
// Rest of your code...
}; This approach uses the Also, please ensure that you're using a version of Node.js that supports ECMAScript modules. The current version of Node.js that langchainjs supports is 20 and above. You can find more details about this solution in this issue. If you still want to use CommonJS, you can change the module system from "commonjs" to "esnext" or "es6" in your TypeScript configuration file (tsconfig.cjs.json). This will allow TypeScript to compile your code to use ECMAScript modules instead of CommonJS. Here's how you can modify your tsconfig.cjs.json: {
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"declaration": false
},
"exclude": [
"node_modules",
"dist",
"docs",
"**/tests"
]
} Please note that this change might affect the compatibility of your code with older environments that do not support ECMAScript modules. If you need to support such environments, you might need to use a tool like Babel to transpile your code to a compatible format. You can find more details about this solution in the langchain/tsconfig.cjs.json file. I hope this helps! If you have any other questions, feel free to ask. 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. |
How are you importing your code? If you're using CJS, you'll need to use const { OpenAI } = require("langchain/llms/openai"); Please reopen if that doesn't fix it! |
when developing with TypeScript ,I got a type error:
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("langchain/tools/gmail")' call instead. To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field "type": "module" to '/demo/package.json'.ts(1479)
The
tsconfig.json
configuration in my project is as follows:The text was updated successfully, but these errors were encountered: