Skip to content

Conversation

@jkossis
Copy link

@jkossis jkossis commented Jan 26, 2026

Current Behavior

When using runtimeDependencies in the webpack config, the build fails if the specified package has an exports field that doesn't include ./package.json:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports"

Expected Behavior

The build should succeed. The package.json file exists and is readable via the filesystem—it's only inaccessible via require.resolve() due to the exports restriction.

Fix

Catch the ERR_PACKAGE_PATH_NOT_EXPORTED error and fall back to resolving the main entry point, then locate package.json via filesystem path manipulation.

Example Package

@as-integrations/express5 has this exports field which triggers the bug:

"exports": {
  ".": {
    "import": "./dist/esm/index.js",
    "require": "./dist/cjs/index.js"
  }
}

Note: ./package.json is not exported, but the file exists and is readable.

Reproduction

  1. Create an Nx workspace with a Node app using webpack
  2. Add a runtime dependency that has strict exports (e.g., @as-integrations/express5)
  3. Configure webpack:
new NxAppWebpackPlugin({
  // ...
  generatePackageJson: true,
  runtimeDependencies: ["@as-integrations/express5"],
})
  1. Run the build → fails with ERR_PACKAGE_PATH_NOT_EXPORTED

@jkossis jkossis requested a review from a team as a code owner January 26, 2026 14:35
@jkossis jkossis requested a review from jaysoo January 26, 2026 14:35
@netlify
Copy link

netlify bot commented Jan 26, 2026

👷 Deploy request for nx-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 104a1ea

@vercel
Copy link

vercel bot commented Jan 26, 2026

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

Project Deployment Review Updated (UTC)
nx-dev Ready Ready Preview Jan 26, 2026 2:55pm

Request Review

depPkgJson = require.resolve(`${dep}/package.json`);
} catch {
// Fallback for packages with strict exports that don't expose ./package.json
const depMain = require.resolve(dep);
Copy link
Contributor

Choose a reason for hiding this comment

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

Unhandled error: If require.resolve(dep) fails inside the catch block (e.g., the dependency doesn't exist or has other resolution issues), the error will propagate uncaught and crash the build. While this may be intentional for truly missing dependencies, it creates inconsistent error handling - the outer try-catch suggests all resolution errors should be handled gracefully.

Fix: Wrap the fallback resolution in its own try-catch:

try {
  depPkgJson = require.resolve(`${dep}/package.json`);
} catch {
  try {
    const depMain = require.resolve(dep);
    const nodeModulesIndex = depMain.lastIndexOf('node_modules');
    if (nodeModulesIndex === -1) continue;
    depPkgJson = path.join(
      depMain.slice(0, nodeModulesIndex),
      'node_modules',
      dep,
      'package.json'
    );
  } catch {
    continue; // Skip if package cannot be resolved at all
  }
}

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Copy link
Author

Choose a reason for hiding this comment

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

The error was previously propagating uncaught, this is just to handle this one case.

Packages with strict `exports` fields that don't expose `./package.json`
cause the build to fail with ERR_PACKAGE_PATH_NOT_EXPORTED when used in
`runtimeDependencies`.

This fix catches the error and falls back to resolving the main entry
point, then locates package.json via filesystem path manipulation.

Example package that triggers this: `@as-integrations/express5`
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.

1 participant