Skip to content

Conversation

rggammon
Copy link

@rggammon rggammon commented Aug 29, 2025

Fix: Add missing root export to @langchain/core package.json

Problem

The @langchain/core package currently has a configuration issue that prevents it from being used with bundlers like Vite when using static manualChunks.

Error encountered:

Failed to resolve entry for package "@langchain/core". The package may have incorrect main/module/exports specified in its package.json: Missing "." specifier in "@langchain/core" package

Root Cause

The package.json has:

  • "main": "./index.js" and "types": "./index.d.ts" pointing to non-existent files
  • ❌ Missing root "." export in the exports field

This prevents bundlers from resolving the package when trying to create explicit chunks.

Solution

  1. Create proper root entry point (src/index.ts) with clear documentation
  2. Add root export to exports field for modern bundler compatibility
  3. Keep main/types fields for maximum compatibility with legacy tools
  4. Guide users to subpath imports for optimal tree-shaking

Changes

  "main": "./index.js",
  "types": "./index.d.ts",
  
  "exports": {
+   ".": {
+     "types": "./index.d.ts",
+     "import": "./index.js", 
+     "require": "./index.cjs"
+   },
    "./agents": {
      // ... existing exports

New src/index.ts:

/**
 * @langchain/core root entry point
 * 
 * This file exists solely to satisfy bundler requirements for packages that use
 * static manual chunks (e.g., Vite's manualChunks, Webpack's splitChunks).
 * 
 * IMPORTANT: Do not import from this root entry point in your code.
 * Instead, use specific subpath imports for better tree-shaking and performance:
 * 
 * ❌ Don't do this:
 * import { BaseMessage } from "@langchain/core";
 * 
 * ✅ Do this instead:
 * import { BaseMessage } from "@langchain/core/messages";
 * import { BaseChatModel } from "@langchain/core/language_models/chat_models";
 */

// Empty export to make this a valid ES module
export {};

## Validation
✅ **Tested with Vite static manualChunks**
```typescript
// This now works:
manualChunks: {
  langchain: ["@langchain/core", "@langchain/ollama"]
}

Build succeeds and creates proper code splitting
Backwards compatible - all existing subpath imports continue to work
Self-documenting - clear comments explain purpose and best practices
Proper architecture - empty root export reinforces subpath-only design

Impact

  • Fixes bundler compatibility for tools like Vite, Rollup, Webpack
  • Enables code splitting and chunk optimization for LangChain packages
  • Maintains compatibility with existing usage patterns
  • No breaking changes to the public API

Related Issues

Testing

This fix has been validated by:

  1. Creating a local patch with the same changes
  2. Testing with Vite build using static manualChunks
  3. Confirming successful chunk separation (langchain bundle: 424.78 kB) putting subpaths as the chunks is required, see comment
  4. Verifying all existing imports continue to work

- Add src/index.ts entry point with clear documentation
- Add root '.' export to exports field for bundler compatibility
- Keep main/types fields for maximum legacy tool compatibility
- Document that root import should be avoided in favor of subpaths

The new index.ts includes comprehensive comments explaining:
- Its purpose (bundler compatibility only)
- Best practices (use subpath imports)
- Examples of correct vs incorrect usage

Fixes static manualChunks resolution in bundlers like Vite, Rollup, and Webpack
while maintaining the intended subpath-only usage pattern and full backwards compatibility.
Copy link

vercel bot commented Aug 29, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Ready Ready Preview Comment Aug 29, 2025 10:54pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Aug 29, 2025 10:54pm

@rggammon
Copy link
Author

To get this fully working vite and manualChunks, putting subpaths in is needed or the subpaths aren't picked up with the static manualChunks approach - but maybe it is worthwhile to have an index.ts for general correctness?

      langchain: [
        "@langchain/core/messages",
        "@langchain/core/language_models/chat_models", 
        "@langchain/core/outputs",
        "@langchain/core/callbacks/manager"
      ],

Copy link
Member

@hntrl hntrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for flagging and for the PR @rggammon!

but maybe it is worthwhile to have an index.ts for general correctness?

My intuition is that everything that's exported from subpaths is also available from the root export. I think we're fine to ship this as is since it's fixing an existing bundler requirement, and we'll look to follow conventional patterns for organizing these barrel files as apart of our v1 effort.

@hntrl
Copy link
Member

hntrl commented Sep 1, 2025

We didn't have perms to modify this branch, so I'm continuing this on in #8825

@hntrl hntrl closed this Sep 1, 2025
@rggammon
Copy link
Author

rggammon commented Sep 2, 2025

thank you, yes exporting everything would be a great solution as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants