Skip to content

Commit bb572d5

Browse files
committed
test: added the requireStack to loader.js
1 parent f8bf498 commit bb572d5

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,15 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
459459
err.code = 'MODULE_NOT_FOUND';
460460
err.path = path.resolve(requestPath, 'package.json');
461461
err.requestPath = originalPath;
462-
// TODO(BridgeAR): Add the requireStack as well.
462+
const requireStack = [];
463+
for (let cursor = module.parent;
464+
cursor;
465+
cursor = moduleParentCache.get(cursor)) {
466+
ArrayPrototypePush(requireStack, cursor.filename || cursor.id);
467+
}
468+
if (requireStack.length > 0) {
469+
err.requireStack = requireStack;
470+
}
463471
throw err;
464472
} else {
465473
const jsonPath = path.resolve(requestPath, 'package.json');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
const fs = require('fs');
8+
const tmpdir = require('../common/tmpdir');
9+
10+
tmpdir.refresh();
11+
12+
fs.writeFileSync(path.join(tmpdir.path, 'index.js'), 'require("a");');
13+
fs.mkdirSync(path.join(tmpdir.path, 'node_modules'));
14+
fs.mkdirSync(path.join(tmpdir.path, 'node_modules/a'));
15+
fs.mkdirSync(path.join(tmpdir.path, 'node_modules/b'));
16+
fs.writeFileSync(path.join(tmpdir.path, 'node_modules/a/a.js'), 'require("b");');
17+
fs.writeFileSync(path.join(tmpdir.path, 'node_modules/a/package.json'), '{ "main": "a.js" }');
18+
fs.writeFileSync(path.join(tmpdir.path, 'node_modules/b/package.json'), '{ "main": "b.js" }');
19+
20+
assert.throws(() => {
21+
require(path.join(tmpdir.path, 'index.js'));
22+
}, {
23+
requireStack: ['/path/to/tmpdir/node_modules/a/a.js', '/path/to/tmpdir/index.js'],
24+
});

0 commit comments

Comments
 (0)