Skip to content
Merged
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
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>
  • Loading branch information
bnoordhuis committed Dec 9, 2015
commit 213ede6ceef03079f2ca1a92dd006e92338a698a
3 changes: 3 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const Module = require('module');
const domain = require('domain');
const debug = util.debuglog('repl');

const parentModule = module;
const replMap = new WeakMap();

try {
Expand Down Expand Up @@ -526,6 +527,8 @@ REPLServer.prototype.createContext = function() {
}

const module = new Module('<repl>');
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];

const require = internalModule.makeRequireFunction.call(module);
context.module = module;
context.require = require;
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/node_modules/baz/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions test/parallel/test-repl-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const net = require('net');

process.chdir(common.fixturesDir);
const repl = require('repl');

const server = net.createServer(conn => {
repl.start('', conn).on('exit', () => {
conn.destroy();
server.close();
});
});

const host = common.localhostIPv4;
const port = common.PORT;
const options = { host, port };

var answer = '';
server.listen(options, function() {
const conn = net.connect(options);
conn.setEncoding('utf8');
conn.on('data', data => answer += data);
conn.write('require("baz")\n.exit\n');
});

process.on('exit', function() {
assert.strictEqual(false, /Cannot find module/.test(answer));
assert.strictEqual(false, /Error/.test(answer));
assert.strictEqual(true, /eye catcher/.test(answer));
});