Skip to content

Commit 213ede6

Browse files
committed
repl: fix require('3rdparty') regression
Fix module loading of third-party modules in the REPL by inheriting module.paths from the REPL's parent module. Commit ee72ee7 ("module,repl: remove repl require() hack") introduced a regression where require() of modules in node_modules directories no longer worked in the REPL (and fortunately only in the REPL.) It turns out we didn't have test coverage for that but we do now. Fixes: #4208 PR-URL: #4215 Reviewed-By: Roman Reiss <[email protected]>
1 parent 931ab96 commit 213ede6

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/repl.js

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Module = require('module');
3535
const domain = require('domain');
3636
const debug = util.debuglog('repl');
3737

38+
const parentModule = module;
3839
const replMap = new WeakMap();
3940

4041
try {
@@ -526,6 +527,8 @@ REPLServer.prototype.createContext = function() {
526527
}
527528

528529
const module = new Module('<repl>');
530+
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];
531+
529532
const require = internalModule.makeRequireFunction.call(module);
530533
context.module = module;
531534
context.require = require;

test/fixtures/node_modules/baz/index.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/parallel/test-repl-require.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
7+
process.chdir(common.fixturesDir);
8+
const repl = require('repl');
9+
10+
const server = net.createServer(conn => {
11+
repl.start('', conn).on('exit', () => {
12+
conn.destroy();
13+
server.close();
14+
});
15+
});
16+
17+
const host = common.localhostIPv4;
18+
const port = common.PORT;
19+
const options = { host, port };
20+
21+
var answer = '';
22+
server.listen(options, function() {
23+
const conn = net.connect(options);
24+
conn.setEncoding('utf8');
25+
conn.on('data', data => answer += data);
26+
conn.write('require("baz")\n.exit\n');
27+
});
28+
29+
process.on('exit', function() {
30+
assert.strictEqual(false, /Cannot find module/.test(answer));
31+
assert.strictEqual(false, /Error/.test(answer));
32+
assert.strictEqual(true, /eye catcher/.test(answer));
33+
});

0 commit comments

Comments
 (0)