Skip to content

Commit d7b9b66

Browse files
committed
Feedback
1 parent 2ba189b commit d7b9b66

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ function trySelf(parentPath, request, conditions) {
662662
function tryPackageMapResolveCJS(request, parent, conditions) {
663663
if (!hasPackageMap()) { return undefined; }
664664

665+
// If the resolution has no parent, use the current working directory. This
666+
// usually happens when running `node -e 'require("some-package")'`.
665667
const parentPath = parent?.filename ?? process.cwd() + path.sep;
666668
const mapped = packageMapResolve(request, parentPath);
667669

lib/internal/modules/package_map.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
} = primordials;
1111

1212
const assert = require('internal/assert');
13-
const { emitExperimentalWarning, getLazy } = require('internal/util');
13+
const { emitExperimentalWarning, getLazy, setOwnProperty } = require('internal/util');
1414
const { resolve: pathResolve, dirname } = require('path');
1515
const { fileURLToPath } = require('internal/url');
1616

@@ -211,10 +211,9 @@ function loadPackageMap(configPath) {
211211
try {
212212
content = fs.readFileSync(configPath);
213213
} catch (err) {
214-
if (err.code === 'ENOENT') {
215-
throw new ERR_PACKAGE_MAP_INVALID(configPath, 'file not found');
216-
}
217-
throw new ERR_PACKAGE_MAP_INVALID(configPath, err.message);
214+
const error = new ERR_PACKAGE_MAP_INVALID(configPath, 'failed to read');
215+
setOwnProperty(error, 'cause', err);
216+
throw error;
218217
}
219218

220219
// Resolve symlinks so that stored paths match the realpath'd module paths
@@ -225,7 +224,9 @@ function loadPackageMap(configPath) {
225224
try {
226225
data = JSONParse(content);
227226
} catch (err) {
228-
throw new ERR_PACKAGE_MAP_INVALID(configPath, `invalid JSON: ${err.message}`);
227+
const error = new ERR_PACKAGE_MAP_INVALID(configPath, `invalid JSON`);
228+
setOwnProperty(error, 'cause', err);
229+
throw error;
229230
}
230231

231232
return new PackageMap(configPath, data);
@@ -249,6 +250,9 @@ function getPackageMap() {
249250
try {
250251
packageMap = loadPackageMap(pathResolve(packageMapPath));
251252
} catch (err) {
253+
// Fallback to an empty package map to avoid repeatedly trying
254+
// to load the broken package map file. Subsequent resolutions
255+
// will still fail to resolve due to the package map being empty.
252256
packageMap = new PackageMap(packageMapPath, { packages: {} });
253257
throw err;
254258
}

test/es-module/test-esm-package-map.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('ESM: --experimental-package-map', () => {
160160
], { cwd: fixtures.path('package-map/root') });
161161

162162
assert.match(stderr, /ERR_PACKAGE_MAP_INVALID/);
163-
assert.match(stderr, /not found/);
163+
assert.match(stderr, /no such file or directory/);
164164
assert.notStrictEqual(code, 0, stderr);
165165
});
166166

0 commit comments

Comments
 (0)