Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 213ede6

Browse filesBrowse 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 <me@silverwind.io>
1 parent 931ab96 commit 213ede6
Copy full SHA for 213ede6

File tree

Expand file treeCollapse file tree

3 files changed

+38
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+38
-0
lines changed
Open diff view settings
Collapse file

‎lib/repl.js‎

Copy file name to clipboardExpand all lines: lib/repl.js
+3Lines changed: 3 additions & 0 deletions
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;
Collapse file

‎test/fixtures/node_modules/baz/index.js‎

Copy file name to clipboardExpand all lines: test/fixtures/node_modules/baz/index.js
+2Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎test/parallel/test-repl-require.js‎

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.