From 41882e4077f5938c9b969024f4a73581496c0d83 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 9 Dec 2015 21:55:29 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/4208 PR-URL: https://github.com/nodejs/node/pull/4215 Reviewed-By: Roman Reiss --- lib/repl.js | 3 +++ test/fixtures/node_modules/baz/index.js | 2 ++ test/parallel/test-repl-require.js | 33 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 test/parallel/test-repl-require.js diff --git a/lib/repl.js b/lib/repl.js index 80ec4defaf1646..84ac726e1f1671 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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 { @@ -512,6 +513,8 @@ REPLServer.prototype.createContext = function() { } const module = new Module(''); + module.paths = Module._resolveLookupPaths('', parentModule)[1]; + const require = internalModule.makeRequireFunction.call(module); context.module = module; context.require = require; diff --git a/test/fixtures/node_modules/baz/index.js b/test/fixtures/node_modules/baz/index.js index 346f068ec52a2b..859ea0924d6fb7 100644 --- a/test/fixtures/node_modules/baz/index.js +++ b/test/fixtures/node_modules/baz/index.js @@ -6,3 +6,5 @@ assert.equal(require('bar'), require('../bar.js')); // this should work, and get the one in ./node_modules/asdf.js assert.equal(require('asdf'), require('./node_modules/asdf.js')); + +module.exports = 'eye catcher'; diff --git a/test/parallel/test-repl-require.js b/test/parallel/test-repl-require.js new file mode 100644 index 00000000000000..e8e5e34190c464 --- /dev/null +++ b/test/parallel/test-repl-require.js @@ -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)); +});