langchain-ai / langchainjs

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

Issue importing submodules using es6 module import from documentation #45

Closed dantheman0207 closed 1 year ago

dantheman0207 commented 1 year ago

Running this code:

import { initializeAgentExecutor } from "langchain/agents";

gives me this error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/USERXXX/Projects/langchain/prototype/javascript/node_modules/langchain/agents' imported from /Users/USERXXX/Projects/langchain/prototype/javascript/prototype.js
Did you mean to import langchain/prototype/javascript/node_modules/langchain/agents.js?

When I add the .js to the import (like this: import { initializeAgentExecutor } from "langchain/agents.js";) it works fine. This same issue occurs for all imports I've tried. It's been a while since I worked with javascript so maybe I'm missing something in the es6 module import process that's obvious but wondering if I'm doing something wrong here or whether the documentation should be updated. I assume it should work without the .js suffix.

Running node v18.13.0 on macOS Catalina 10.15.

nfcampos commented 1 year ago

We're missing the exports key, thanks for pointing it out! we'll get it fixed

sullivan-sean commented 1 year ago

Hey there! Would love to get a little bit more info on your setup for this project. Are you using js or typescript? If typescript, could you share your tsconfig?

We used to use the exports approach, but this doesn't work with the default moduleResolution: "node" configuration (see https://github.com/microsoft/TypeScript/issues/50794#issue-1375081777). Instead we take an approach similar to nextjs where we generate a bunch of top level .js entrypoints (e.g. files like this file in the next repo)

Ideally we could continue using this approach - I have high confidence we can make it work given this is what next is doing.

You can also check out chat langchain which is an example project that imports from subpaths

dantheman0207 commented 1 year ago

Thanks! I will look at the example project more. I am not using typescript, just vanilla node. The only change I made after running node init and autogenerating my package.json was adding a "type": "module", field. Here is my (lightly redacted) package.json:


{
  "name": "prototype",
  "version": "1.0.0",
  "description": "prototype of langchain in js",
  "main": "prototype.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node prototype.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/dantheman0207/XXX.git"
  },
  "author": "XXX",
  "bugs": {
    "url": "https://github.com/dantheman0207/XXX/issues"
  },
  "homepage": "https://github.com/dantheman0207/XXX#readme",
  "dependencies": {
    "langchain": "^0.0.7",
    "openai": "^3.1.0",
    "serpapi": "^1.1.1"
  }
}
sullivan-sean commented 1 year ago

Hoping this will be solved by https://github.com/hwchase17/langchainjs/pull/54 -- I'll test with your package.json here before we merge

EDIT: a very simple repo with your package.json and import statement from above is working for me locally after that PR

sullivan-sean commented 1 year ago

This is live in version 0.0.8 - could you just double check if this is resolved on your end before I close?

sullivan-sean commented 1 year ago

Closing for now, can re-open if anyone is still encountering this issue

dantheman0207 commented 1 year ago

Sorry for the delay, couldn't get to this. Works for me!