module: report unreadable package.json

A package.json that exists but cannot be read was treated the same as
one that is not there: the read failure returned no config and
resolution continued as if the package had none. Fields such as
"exports" and "type" silently disappear, so a specifier can resolve to
a different file than the package declares, while an unparsable
package.json already throws ERR_INVALID_PACKAGE_CONFIG.

Keep treating ENOENT and ENOTDIR as "no package config here", and
report any other read failure with the underlying error.

Fixes: https://github.com/nodejs/node/issues/65220
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
PR-URL: https://github.com/nodejs/node/pull/65223
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Paul Bouchon authored and GitHub committed 2026-08-20 23:56:07 +00:00
1 parent 0ce7b02d50
commit c4cfa637d3
2 files changed
+74 -1

No files matched your search

+17 -1
View File
@@ -111,7 +111,23 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
PackageConfig package_config{};
package_config.file_path = path;
// No need to exclude BOM since simdjson will skip it.
if (ReadFileSync(&package_config.raw_json, path.data()) < 0) {
int read_error = ReadFileSync(&package_config.raw_json, path.data());
if (read_error < 0) {
// No file at this path, a path component that is not a directory, or a
// "package.json" that is itself a directory all mean there is no package
// config here. Any other failure means a package.json is present but
// could not be read. Treating that as absent silently drops fields such
// as "exports" and "type", which can resolve a specifier to a different
// file, so surface the read error instead of continuing.
if (read_error != UV_ENOENT && read_error != UV_ENOTDIR &&
read_error != UV_EISDIR) {
THROW_ERR_INVALID_PACKAGE_CONFIG(realm->isolate(),
"Cannot read package config %s: %s.",
path.data(),
uv_strerror(read_error));
return nullptr;
}
// Add `nullopt` to the package config cache so that we don't
// need to open and attempt to read this path again
binding_data->package_configs_.insert({std::string(path), std::nullopt});