Skip to content

Commit 848b04b

Browse files
committed
node: allow preload modules with -i
This gives us the ability to preload when using the node repl. This can be useful for doing things like creating aliases. Fixes: #4661 PR-URL: #4696 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6988d2e commit 848b04b

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
}
141141

142142
} else {
143+
startup.preloadModules();
143144
// If -i or --interactive were passed, or stdin is a TTY.
144145
if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
145146
// REPL

test/fixtures/define-global.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global.a = 'test';

test/parallel/test-preload.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
55
const child_process = require('child_process');
@@ -21,6 +21,7 @@ var fixture = function(name) {
2121
var fixtureA = fixture('printA.js');
2222
var fixtureB = fixture('printB.js');
2323
var fixtureC = fixture('printC.js');
24+
const fixtureD = fixture('define-global.js');
2425
var fixtureThrows = fixture('throws_error4.js');
2526

2627
// test preloading a single module works
@@ -73,6 +74,18 @@ child_process.exec(nodeBinary + ' '
7374
assert.equal(stdout, 'A\nB\nhello\n');
7475
});
7576

77+
// test that preload works with -i
78+
const interactive = child_process.exec(nodeBinary + ' '
79+
+ preloadOption([fixtureD])
80+
+ '-i',
81+
common.mustCall(function(err, stdout, stderr) {
82+
assert.ifError(err);
83+
assert.strictEqual(stdout, `> 'test'\n> `);
84+
}));
85+
86+
interactive.stdin.write('a\n');
87+
interactive.stdin.write('process.exit()\n');
88+
7689
child_process.exec(nodeBinary + ' '
7790
+ '--require ' + fixture('cluster-preload.js') + ' '
7891
+ fixture('cluster-preload-test.js'),

0 commit comments

Comments
 (0)