langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

Getting ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

Open liorshk opened this issue 2 years ago • 2 comments

We are getting

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in langchain\package.json It started happening from versions 0.0.13 and up. It works fine for 0.0.12.

liorshk avatar Mar 01 '23 21:03 liorshk

Same issue as well. The exports in the package.json have switched from

    "./embeddings": {
      "import": "./embeddings.mjs",
      "default": "./embeddings.js"
    },

to

    "./embeddings": {
      "types": "./embeddings.d.ts",
      "import": "./embeddings.js"
    },

evad1n avatar Mar 01 '23 21:03 evad1n

The langchain package is now ESM only. You can fix that by adding "type": "module" to your package.json. You can find more information here https://hwchase17.github.io/langchainjs/docs/getting-started/

nfcampos avatar Mar 01 '23 21:03 nfcampos

The langchain package is now ESM only. You can fix that by adding "type": "module" to your package.json. You can find more information here https://hwchase17.github.io/langchainjs/docs/getting-started/

@nfcampos I'm not sure I understand why this requirement exists. Is this on purpose or will this be changed? Happy to open up a PR with the fix if that would be accepted.

irl-dan avatar Mar 06 '23 03:03 irl-dan

@irl-dan we converted the package to ESM so that we can support other environments outside of Node, which are ESM only, discussion here https://github.com/hwchase17/langchainjs/discussions/152

You also have the option to use it without type: module by using dynamic import as mentioned here https://hwchase17.github.io/langchainjs/docs/getting-started/#commonjs-in-nodejs

nfcampos avatar Mar 06 '23 07:03 nfcampos

Hello.

Using dynamic import on my side produce the same error.

const { OpenAI } = await import('langchain');

and

const { AnalyzeDocumentChain, loadSummarizationChain } = await import('langchain/chains');

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "resolveJsonModule": true
  }
}

node version:

v18.12.1

Any idea ?

Note that changing es2017 to es2020 do not change anything to the problem.

sboudouk avatar Mar 07 '23 21:03 sboudouk

Some more information on your issue here https://stackoverflow.com/questions/65265420/how-to-prevent-typescript-from-transpiling-dynamic-imports-into-require

A solution for you appears to be "moduleResolution": "node16", see here https://github.com/microsoft/TypeScript/issues/43329#issuecomment-1315512792

nfcampos avatar Mar 07 '23 21:03 nfcampos

Thanks for the fast reply.

Doing so, with :

    const { OpenAI } = await import_('langchain');
    const { AnalyzeDocumentChain, loadSummarizationChain } = await import_('langchain/chains');
    const { RecursiveCharacterTextSplitter } = await import_('langchain/text_splitter');

    const text = this.speakerRecognitionPromptFormatting(
      JSON.parse((conversation as any).toString()).prediction,
      'ok',
    );

    // text is a string.
    
    const model = new OpenAI({
      temperature: 0,
      openAIApiKey: ...,
    });
    /** Load the summarization chain. */
    const chain = loadSummarizationChain(model);
    const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
    const docs = await textSplitter.createDocuments([text]);
    const res = await chain.call({
      input_document: docs,
    });

Results in a Error: Document key input_documents not found. error. Also tried with AnalyzeDocumentChain from documentation snippet.

Do you want me to create a separate issue ?

sboudouk avatar Mar 07 '23 21:03 sboudouk

I think the issue might be an s missing on input_document on the last line, should be input_documents

nfcampos avatar Mar 07 '23 22:03 nfcampos

Yep it is. Thanks. Can I submit a PR on the documentation for this single simple correction ?

sboudouk avatar Mar 07 '23 22:03 sboudouk

Yes, thank you

nfcampos avatar Mar 07 '23 22:03 nfcampos

Unfortunately It might not be only a documentation issue, running :

    const text = this.speakerRecognitionPromptFormatting(
      JSON.parse((conversation as any).toString()).prediction,
      'oki',
    );
    console.log('text :', text);
    const model = new OpenAI({
      temperature: 0,
      openAIApiKey: ...,
    });
    /** Load the summarization chain. */
    const combineDocsChain = loadSummarizationChain(model);
    /** Pass this into the AnalyzeDocumentChain. */
    const chain = new AnalyzeDocumentChain({
      combineDocumentsChain: combineDocsChain,
    });
    const res = await chain.call({
      input_documents: text,
    });

Regarding input_documents or input_document with the s change I get different errors. By trying to match the code I get hit by TypeError: currentDocs.map is not a function, occuring in combine_docs_chain.js

I feel like I might be doing something wrong. Note that the text that i'm sending is just a string, not coming from any file.

sboudouk avatar Mar 07 '23 22:03 sboudouk

Ah, input_documents is expected to be an array of Document, see eg the second code block in this page https://hwchase17.github.io/langchainjs/docs/modules/chains/summarization You can get an array of Documents from eg a TextSplitter, or a Loader

nfcampos avatar Mar 07 '23 22:03 nfcampos

When will langchain support CommonJS? Turning my backend package into a module is problematic for many other packages that rely on CommonJS patterns.

ztratar avatar Mar 08 '23 04:03 ztratar

If it can helps someone running CommonJS (pretty common to be fair), you can import the langchains modules dynamically and without changing anything config related by using this import package

https://www.npmjs.com/package/@brillout/import

sboudouk avatar Mar 08 '23 14:03 sboudouk

I was struggling with this in a NextJS application. Fortunately, I'm in a monorepo environment and I was able to get it to work by creating a new "package" with type: "module and then having NextJS transpile the package.

My interface to langchain goes through the new package and NextJS is happy.

next.config.js

{
   transpilePackages: ["my-ai-package-w-langchain"],
};

sethgw avatar Mar 12 '23 06:03 sethgw

I was able to get around by the following codes (w/o type: "module" in package.json):

// tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "nodenext"
  }
}
// main.ts

const main = async () => {
  const { OpenAI } = await import('langchain');

  const llm = new OpenAI({
    openAIApiKey: '-----',
  });

  const res = await llm.call(
    'your prompt'
  );
  console.log(res);
};

main()
  .then(() => {
    console.log('[done]');
  })
  .catch((e) => {
    console.error(e);
  });
% ts-node path/to/main.ts

tmgauss avatar Mar 14 '23 06:03 tmgauss

Just hitting this now, +1 to CommonJS support. The only workaround I found that "worked" is brillout, mentioned above

import { import_ } from '@brillout/import';

async function main() {
  const { OpenAI } = await import_('langchain');
  const model = new OpenAI({ openAIApiKey: process.env.OPENAI_API_KEY!, temperature: 0.9 });
  const res = await model.call(
    'What would be a good company name a company that makes colorful socks?',
  );
  console.log(res);
}

Granted, I cannot get it to actually send requests...

ReferenceError: Headers is not defined
    at createRequest (file:///Users/nickbradford/dev/node_modules/langchain/src/util/axios-fetch-adapter.js:273:19)
    at fetchAdapter (file:///Users/nickbradford/dev/node_modules/langchain/src/util/axios-fetch-adapter.js:196:19)
    at dispatchRequest (/Users/nickbradford/dev/node_modules/axios/lib/core/dispatchRequest.js:58:10)
    at Axios.request (/Users/nickbradford/dev/node_modules/axios/lib/core/Axios.js:108:15)
    at Function.wrap [as request] (/Users/nickbradford/dev/node_modules/axios/lib/helpers/bind.js:9:15)
    at /Users/nickbradford/dev/node_modules/openai/dist/common.js:149:22
    at /Users/nickbradford/dev/node_modules/openai/dist/api.js:1738:133

nsbradford avatar Mar 22 '23 00:03 nsbradford

@nfcampos Encounter the same condition...

wukaipeng-dev avatar Mar 22 '23 12:03 wukaipeng-dev

@nfcampos I found the reason is that we missing the polyfill to node-fetch:

import { import_ } from '@brillout/import'
import { Logger } from '@nestjs/common'

export const openaiTranslate = async (text: string, target: string[]) => {
  // Polyfill for node-fetch
  const  { Headers, Request, Response } = await import_('node-fetch')
  const fetch = await import_('node-fetch').then((mod) => mod.default)
  if (!globalThis.fetch) globalThis.fetch = fetch
  if (!globalThis.Headers) globalThis.Headers = Headers
  if (!globalThis.Request) globalThis.Request = Request
  if (!globalThis.Response) globalThis.Response = Response

  const { OpenAI } = await import_('langchain')
  const OPENAI_API_KEY = 'sk-xxx'
  const model = new OpenAI({ openAIApiKey: OPENAI_API_KEY, temperature: 0.9 })

  const res = await model.call(
    'What would be a good company name a company that makes colorful socks?'
  )

  Logger.log(res)

  return res
}

wukaipeng-dev avatar Mar 22 '23 13:03 wukaipeng-dev

I think where this is confusing is that the docs say for commonjs modules to use async import but commonjs modules are not allowed to await calls at the top level of any module. This makes the docs feel a bit counterintuitive

kdawgwilk avatar Mar 29 '23 17:03 kdawgwilk

@irl-dan we converted the package to ESM so that we can support other environments outside of Node, which are ESM only, discussion here #152

You also have the option to use it without type: module by using dynamic import as mentioned here https://hwchase17.github.io/langchainjs/docs/getting-started/#commonjs-in-nodejs

Page not found.

dangell7 avatar Mar 29 '23 22:03 dangell7

Updated link https://js.langchain.com/docs/getting-started/install

nfcampos avatar Mar 30 '23 05:03 nfcampos

Would there be interest in exporting to commons and esm? I'd be interested in taking that on.

imrank1 avatar Apr 04 '23 13:04 imrank1

@imrank1 the reason we made the library ESM only is that allows us to use ESM-only dependencies. To add a commonjs export you'd have to review every existing dependency to confirm whether they are not ESM-only

nfcampos avatar Apr 04 '23 14:04 nfcampos

I see thanks.

Curious if anyone here has a working example of nestjs to view. The options mentioned here don't work for me. When using the await method it breaks other areas of nestjs. I'll post what I have in a bit

imrank1 avatar Apr 04 '23 14:04 imrank1

@imrank1 see an example here https://github.com/sullivan-sean/chat-langchainjs/

Or maybe better to look at my branch updating it to latest langchain version https://github.com/nfcampos/chat-langchainjs/tree/nc/lc0037

nfcampos avatar Apr 04 '23 15:04 nfcampos

@imrank1 see an example here https://github.com/sullivan-sean/chat-langchainjs/

Or maybe better to look at my branch updating it to latest langchain version https://github.com/nfcampos/chat-langchainjs/tree/nc/lc0037

Thanks, the first reference worked for me.

Your fork off of the latest threw an error for me. Followed the exact steps in README for both.

yarn && yarn ingest 
yarn install v1.22.19
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
✨  Done in 5.35s.
yarn run v1.22.19
$ tsx -r dotenv/config ingest.ts
Loader created.
Docs splitted.
Creating vector store...
/Users/imrankhawaja/chat-langchainjs/node_modules/langchain/dist/vectorstores/hnswlib.js:44
        return new HierarchicalNSW(args.space, args.numDimensions);
               ^


Error: Wrong space name, expected "l2" or "ip".
    at Function.getHierarchicalNSW (/Users/imrankhawaja/chat-langchainjs/node_modules/langchain/dist/vectorstores/hnswlib.js:44:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at HNSWLib.initIndex (/Users/imrankhawaja/chat-langchainjs/node_modules/langchain/dist/vectorstores/hnswlib.js:51:26)
    at HNSWLib.addVectors (/Users/imrankhawaja/chat-langchainjs/node_modules/langchain/dist/vectorstores/hnswlib.js:70:9)
    at Function.fromDocuments (/Users/imrankhawaja/chat-langchainjs/node_modules/langchain/dist/vectorstores/hnswlib.js:147:9)
    at run (/Users/imrankhawaja/chat-langchainjs/ingest.ts:77:23)
    at <anonymous> (/Users/imrankhawaja/chat-langchainjs/ingest.ts:82:3)

Let me know if should log an issue in your fork?

imrank1 avatar Apr 04 '23 16:04 imrank1

@imrank1 ah thanks for letting me know, I've pushed a commit fixing that. the version of hnswlib-node needed to be updated

nfcampos avatar Apr 04 '23 16:04 nfcampos

@imrank1 ah thanks for letting me know, I've pushed a commit fixing that. the version of hnswlib-node needed to be updated

Verified it works!

imrank1 avatar Apr 04 '23 17:04 imrank1

Also found this problematic given that top level awaits aren't allowed. It prevents extending any of the base classes like below:

const { BaseDocumentLoader } = await import('langchain/document_loaders');

export default class SanitizeHtmlLoader extends BaseDocumentLoader {
}

brishin avatar Apr 05 '23 03:04 brishin