Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const requireUtil = createRequireFromPath('../src/utils/');
requireUtil('./some-tool');
```

### `module.isPreloading`
<!-- YAML
added: REPLACEME
-->

* Type: {boolean} `true` if the module is running during the Node.js preload
phase.

### `module.syncBuiltinESMExports()`
<!-- YAML
added: v12.12.0
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const relativeResolveCache = ObjectCreate(null);

let requireDepth = 0;
let statCache = null;
let isPreloading = false;

function stat(filename) {
filename = path.toNamespacedPath(filename);
Expand Down Expand Up @@ -231,6 +232,10 @@ ObjectDefineProperty(Module, 'wrapper', {
}
});

ObjectDefineProperty(Module.prototype, 'isPreloading', {
get() { return isPreloading; }
});

function getModuleParent() {
return moduleParentCache.get(this);
}
Expand Down Expand Up @@ -1243,6 +1248,8 @@ Module._preloadModules = function(requests) {
if (!ArrayIsArray(requests))
return;

isPreloading = true;

// Preloaded modules have a dummy parent module which is deemed to exist
// in the current working directory. This seeds the search path for
// preloaded modules.
Expand All @@ -1251,11 +1258,13 @@ Module._preloadModules = function(requests) {
parent.paths = Module._nodeModulePaths(process.cwd());
} catch (e) {
if (e.code !== 'ENOENT') {
isPreloading = false;
throw e;
}
}
for (let n = 0; n < requests.length; n++)
parent.require(requests[n]);
isPreloading = false;
};

Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ispreloading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const assert = require('assert');
assert(module.isPreloading);
13 changes: 13 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ const fixtureE = fixtures.path('intrinsic-mutation.js');
const fixtureF = fixtures.path('print-intrinsic-mutation-name.js');
const fixtureG = fixtures.path('worker-from-argv.js');
const fixtureThrows = fixtures.path('throws_error4.js');
const fixtureIsPreloading = fixtures.path('ispreloading.js');

// Assert that module.isPreloading is false here
assert(!module.isPreloading);

// Test that module.isPreloading is set in preloaded module
// Test preloading a single module works
childProcess.exec(
`"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`,
function(err, stdout, stderr) {
assert.ifError(err);
assert.strictEqual(stdout, 'B\n');
});

// Test preloading a single module works
childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`,
Expand Down