langchain-ai / langchainjs

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

Bug: @langchain/mistralai - Error [ERR_REQUIRE_ESM]: require() of ES Module #3662

Closed gkhngyk closed 7 months ago

gkhngyk commented 7 months ago

When I try to import ChatMistralAI I get the following error:

/Users/Desktop/xamax/server/node_modules/@langchain/mistralai/dist/chat_models.cjs:8
const mistralai_1 = __importDefault(require("@mistralai/mistralai"));
                                    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/Desktop/xamax/server/node_modules/@mistralai/mistralai/src/client.js from /Users/Desktop/xamax/server/node_modules/@langchain/mistralai/dist/chat_models.cjs not supported.
Instead change the require of client.js in /Users/Desktop/xamax/server/node_modules/@langchain/mistralai/dist/chat_models.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/Desktop/xamax/server/node_modules/@langchain/mistralai/dist/chat_models.cjs:8:37)
    at Object.<anonymous> (/Users/Desktop/xamax/server/node_modules/@langchain/mistralai/dist/index.cjs:17:14)
    at Object.<anonymous> (/Users/Desktop/xamax/server/node_modules/@langchain/mistralai/index.cjs:1:18)
    at Object.<anonymous> (/Users/Desktop/xamax/server/src/flows/index.js:22:27)
    at Object.<anonymous> (/Users/Desktop/xamax/server/src/server.js:75:5) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v19.9.0
dosubot[bot] commented 7 months ago

🤖

Hello @gkhngyk! I'm Dosu, a friendly bot who's here to assist you with pesky bugs, answer your queries, or guide you on how to contribute to our repository. While we're waiting for a human maintainer to join us, I'm here to make your experience smoother! Let's get started, shall we?

The error you're encountering is due to the fact that the @langchain/mistralai package is defined as an ES module, as indicated by the "type": "module" line in its package.json file. This means that it uses the import/export syntax, which is not compatible with the require() function used in CommonJS modules.

However, the package.json file also specifies an alternative entry point for require() calls via the "require": "./index.cjs" line under "exports". This means that you should be able to import the package using require() if you're running your code in a CommonJS environment.

If you're still encountering the error, it could be due to one of the following reasons:

  1. The index.cjs file does not exist or is not correctly exporting the ChatMistralAI object.
  2. Your Node.js version is below 18, as specified by the "engines": { "node": ">=18" } line. Node.js 12.17.0 and later versions support ES modules, but full support, including the "exports" field in package.json, was not added until Node.js 14.

To resolve the issue, you can try the following:

You can refer to the libs/langchain-mistralai/package.json for more details.

Also, there have been similar issues reported in the past, you can refer to these for more context:

Please let me know if this helps or if you need further assistance.

-Dosu

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 commented 7 months ago

CC @bracesproul

bracesproul commented 7 months ago

hey @gkhngyk the issue should be resolved if you bump your @langchain/mistralai package to 0.0.2

gkhngyk commented 7 months ago

hey @gkhngyk the issue should be resolved if you bump your @langchain/mistralai package to 0.0.2

Thank you the problem seems to be solved.

davidfant commented 4 months ago

@bracesproul I'm running into this with @langchain/mistralai 0.0.8:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/fant/repos/ai/godmode/packages/node_modules/@mistralai/mistralai/src/client.js from /Users/fant/repos/ai/godmode/packages/node_modules/@langchain/mistralai/dist/chat_models.cjs not supported.
Instead change the require of client.js in /Users/fant/repos/ai/godmode/packages/node_modules/@langchain/mistralai/dist/chat_models.cjs to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/Users/fant/repos/ai/godmode/packages/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/fant/repos/ai/godmode/packages/node_modules/@langchain/mistralai/dist/chat_models.cjs:7:37)
    at require.extensions.<computed> [as .js] (/Users/fant/repos/ai/godmode/packages/node_modules/ts-node/dist/index.js:851:20) {
  code: 'ERR_REQUIRE_ESM'
}
bracesproul commented 4 months ago

@bracesproul I'm running into this with @langchain/mistralai 0.0.8:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/fant/repos/ai/godmode/packages/node_modules/@mistralai/mistralai/src/client.js from /Users/fant/repos/ai/godmode/packages/node_modules/@langchain/mistralai/dist/chat_models.cjs not supported.
Instead change the require of client.js in /Users/fant/repos/ai/godmode/packages/node_modules/@langchain/mistralai/dist/chat_models.cjs to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/Users/fant/repos/ai/godmode/packages/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/fant/repos/ai/godmode/packages/node_modules/@langchain/mistralai/dist/chat_models.cjs:7:37)
    at require.extensions.<computed> [as .js] (/Users/fant/repos/ai/godmode/packages/node_modules/ts-node/dist/index.js:851:20) {
  code: 'ERR_REQUIRE_ESM'
}

whoops sorry about that, I thought they fixed that with their new sdk update... Fix is here, will be released in a few min: https://github.com/langchain-ai/langchainjs/pull/4561

sneko commented 4 months ago

@bracesproul when trying to use last versions I face this error:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './utils/function_calling' is not defined by "exports" in /.../node_modules/@langchain/core/package.json
    at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
    at new NodeError (node:internal/errors:405:5)
    at exportsNotFound (node:internal/modules/esm/resolve:366:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:713:9)
    at resolveExports (node:internal/modules/cjs/loader:584:36)
    at Module._findPath (node:internal/modules/cjs/loader:658:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1120:27)
    at a._resolveFilename (/.../node_modules/tsx/dist/cjs/index.cjs:1:1729)
    at Module._load (node:internal/modules/cjs/loader:975:27)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/.../node_modules/@langchain/mistralai/dist/chat_models.cjs:8:28)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/.../node_modules/@langchain/mistralai/dist/index.cjs:17:14)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/.../node_modules/@langchain/mistralai/index.cjs:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at fullAnswer (/.../src/features/llm-langchain.ts:6:52)
    at Object.<anonymous> (/.../src/features/llm-langchain.ts:751:1)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at <anonymous> (/.../src/features/llm.ts:4:57)
    at Object.<anonymous> (/.../src/features/llm.ts:75:23)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at <anonymous> (/.../src/server/system.ts:3:36)
    at Object.<anonymous> (/.../src/server/system.ts:18:23)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at https (/.../src/features/domain.ts:26:41)
    at Object.<anonymous> (/.../src/features/domain.ts:1120:23)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at <anonymous> (/.../src/cli/program.ts:11:8)
    at Object.<anonymous> (/.../src/cli/program.ts:235:4)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at <anonymous> (/.../src/cli/index.ts:1:25)
    at Object.<anonymous> (/.../src/cli/index.ts:7:2)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/.../node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:106:12) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Anyone else?

Can someone provides its dependencies versions about langchain? I also tried with https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-mistralai versions but same.

EDIT: similar to https://github.com/langchain-ai/langchainjs/issues/4269

EDIT2: if upgrading @langchain/core I have a new error from the Mistral API ... {"object":"error","message":{"detail":[{"type":"extra_forbidden","loc":["body","tools"],"msg":"Extra inputs are not permitted","input":[],"url":"https://errors.pydantic.dev/2.5/v/extra_forbidden"}]},"type":"invalid_request_error","param":null,"code":null}. It would be great to always have a working example so people know which versions to use.

bracesproul commented 4 months ago

at Module._resolveFilename (node:internal/modules/cjs/loader:1120:27)

Have you installed core according to this page and are on the latest version (0.1.40)?