8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2a66cd3 commit d94f4c2Copy full SHA for d94f4c2
lib/internal/modules/cjs/helpers.js
@@ -11,12 +11,7 @@ function makeRequireFunction(mod) {
11
const Module = mod.constructor;
12
13
function require(path) {
14
- try {
15
- exports.requireDepth += 1;
16
- return mod.require(path);
17
- } finally {
18
- exports.requireDepth -= 1;
19
- }
+ return mod.require(path);
20
}
21
22
function resolve(request, options) {
@@ -139,7 +134,6 @@ module.exports = exports = {
139
134
builtinLibs,
140
135
makeRequireFunction,
141
136
normalizeReferrerURL,
142
- requireDepth: 0,
143
137
stripBOM,
144
138
stripShebang
145
};
lib/internal/modules/cjs/loader.js
@@ -37,7 +37,6 @@ const { safeGetenv } = internalBinding('credentials');
37
const {
38
39
40
- requireDepth,
41
42
43
} = require('internal/modules/cjs/helpers');
@@ -85,18 +84,17 @@ const {
85
84
86
const isWindows = process.platform === 'win32';
87
+let requireDepth = 0;
88
+let statCache = new Map();
89
function stat(filename) {
90
filename = path.toNamespacedPath(filename);
- const cache = stat.cache;
91
- if (cache !== null) {
92
- const result = cache.get(filename);
93
- if (result !== undefined) return result;
94
95
- const result = internalModuleStat(filename);
96
- if (cache !== null) cache.set(filename, result);
+ if (statCache === null) statCache = new Map();
+ let result = statCache.get(filename);
+ if (result !== undefined) return result;
+ result = internalModuleStat(filename);
+ statCache.set(filename, result);
97
return result;
98
99
-stat.cache = null;
100
101
function updateChildren(parent, child, scan) {
102
var children = parent && parent.children;
@@ -702,7 +700,12 @@ Module.prototype.require = function(id) {
702
700
throw new ERR_INVALID_ARG_VALUE('id', id,
703
701
'must be a non-empty string');
704
705
- return Module._load(id, this, /* isMain */ false);
+ requireDepth++;
+ try {
+ return Module._load(id, this, /* isMain */ false);
706
+ } finally {
707
+ requireDepth--;
708
+ }
709
710
711
@@ -785,8 +788,6 @@ Module.prototype._compile = function(content, filename) {
785
788
786
789
var dirname = path.dirname(filename);
787
790
var require = makeRequireFunction(this);
- var depth = requireDepth;
- if (depth === 0) stat.cache = new Map();
791
var result;
792
var exports = this.exports;
793
var thisValue = exports;
@@ -798,7 +799,7 @@ Module.prototype._compile = function(content, filename) {
798
799
result = compiledWrapper.call(thisValue, exports, require, module,
800
filename, dirname);
801
- if (depth === 0) stat.cache = null;
802
+ if (requireDepth === 0) statCache = null;
803
804
805
test/fixtures/module-require-depth/one.js
test/fixtures/module-require-depth/two.js
test/parallel/test-module-require-depth.js