Skip to content

Commit 5f8555a

Browse files
committed
module: check env NODE_REQUIRE for preload modules
Adds a new feature that checks the environment variable NODE_REQUIRE that should be a : (or ; on windows) delimited string of modules to preload, the same as those that can be specified by the -r command line option. Implements: #11853
1 parent d099f8e commit 5f8555a

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

lib/internal/bootstrap_node.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,15 @@
394394

395395
// Load preload modules
396396
function preloadModules() {
397-
if (process._preload_modules) {
398-
NativeModule.require('module')._preloadModules(process._preload_modules);
397+
let preloads = process._preload_modules;
398+
399+
if (process.env.NODE_REQUIRE) {
400+
const d = NativeModule.require('path').delimiter;
401+
preloads = (preloads || []).concat(process.env.NODE_REQUIRE.split(d));
402+
}
403+
404+
if (preloads) {
405+
NativeModule.require('module')._preloadModules(preloads);
399406
}
400407
}
401408

test/parallel/test-preload.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,46 @@ childProcess.exec(
146146
assert.ok(/worker terminated with code 43/.test(stdout));
147147
}
148148
);
149+
150+
// test NODE_REQUIRE with a single module works
151+
process.env.NODE_REQUIRE = fixtureA;
152+
childProcess.exec(nodeBinary + ' ' + fixtureB,
153+
function(err, stdout, stderr) {
154+
assert.ifError(err);
155+
assert.strictEqual(stdout, 'A\nB\n');
156+
});
157+
158+
// test NODE_REQUIRE with multiple modules works
159+
process.env.NODE_REQUIRE = fixtureA + path.delimiter + fixtureB;
160+
childProcess.exec(
161+
nodeBinary + ' ' + fixtureC,
162+
function(err, stdout, stderr) {
163+
assert.ifError(err);
164+
assert.strictEqual(stdout, 'A\nB\nC\n');
165+
}
166+
);
167+
168+
// test NODE_REQUIRE with a throwing module aborts
169+
process.env.NODE_REQUIRE = fixtureA + path.delimiter + fixtureThrows;
170+
childProcess.exec(
171+
nodeBinary + ' ' + fixtureB,
172+
function(err, stdout, stderr) {
173+
if (err) {
174+
assert.strictEqual(stdout, 'A\n');
175+
} else {
176+
throw new Error('NODE_REQUIRE Preload should have failed');
177+
}
178+
}
179+
);
180+
181+
// test mixing NODE_REQUIRE with -r works
182+
process.env.NODE_REQUIRE = fixtureB;
183+
childProcess.exec(
184+
nodeBinary + ' ' + preloadOption([fixtureA]) + ' ' + fixtureC,
185+
function(err, stdout, stderr) {
186+
assert.ifError(err);
187+
assert.strictEqual(stdout, 'A\nB\nC\n');
188+
}
189+
);
190+
191+
delete process.env.NODE_REQUIRE;

0 commit comments

Comments
 (0)