langchainjs: Getting ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

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.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 14
  • Comments: 35 (6 by maintainers)

Most upvoted comments

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/

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

Hi folks, we have an open PR adding CJS support #626. If you want to give it a try you can install the prerelease version with npm i langchain@next. Can you let me know it it works for you, before we announce for everyone?

Hi, we’ve released the new version 0.0.49 which adds a CJS build and fixes this issue.

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

@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 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
}

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

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.

I really can’t get this to work. I’m using 0.0.50. It has issues with typescript it seems.

{
  "include": [
    "src/**/*",
  ],
  "compilerOptions": {
    "target": "es2020", 
    "module": "commonjs",         /* I need this for other modules */
    "outDir": "./dist",
    "strict": true, 
    "baseUrl": "./src",
    "skipLibCheck": true,        /* Remove this when langchain type check works. */
    "typeRoots": [
      "node_modules/@types"
    ], 
    "types": [
      "node",
      "jest"
    ],
    "lib":  [
      "es2020",
      "dom",
    ],=
    "esModuleInterop": true,      /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "inlineSourceMap": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",
    "allowJs": true
  }
}

Anyone else have the same issues?

@nfcampos i tried with langchain-0.0.49 as u mention. Path error solved but not able to use new packages. module not found Screenshot (1220)

@nfcampos Brilliant! I had to uninstall and reinstall d3-dsv (I think because of a version change), but this works well with the following in my tsconfig.json:

{
  "compilerOptions:" {
    // ...
    "module": "commonjs",
    "moduleResolution": "node",
    // ...
  }
}

Thanks a bunch, this makes life a lot easier.

I can confirm this worked for me as well. thanks!

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

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

@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.

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

Yes, thank you