-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(webpack): handle packages with strict exports in runtimeDependencies #34220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(webpack): handle packages with strict exports in runtimeDependencies #34220
Conversation
👷 Deploy request for nx-docs pending review.Visit the deploys page to approve it
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| depPkgJson = require.resolve(`${dep}/package.json`); | ||
| } catch { | ||
| // Fallback for packages with strict exports that don't expose ./package.json | ||
| const depMain = require.resolve(dep); |
There was a problem hiding this comment.
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
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
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`
640ebde to
104a1ea
Compare
Current Behavior
When using
runtimeDependenciesin the webpack config, the build fails if the specified package has anexportsfield that doesn't include./package.json:Expected Behavior
The build should succeed. The
package.jsonfile exists and is readable via the filesystem—it's only inaccessible viarequire.resolve()due to the exports restriction.Fix
Catch the
ERR_PACKAGE_PATH_NOT_EXPORTEDerror and fall back to resolving the main entry point, then locatepackage.jsonvia filesystem path manipulation.Example Package
@as-integrations/express5has this exports field which triggers the bug:Note:
./package.jsonis not exported, but the file exists and is readable.Reproduction
@as-integrations/express5)ERR_PACKAGE_PATH_NOT_EXPORTED