summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZYSzys <zyszys98@gmail.com>2019-12-07 21:19:57 +0800
committerBeth Griggs <Bethany.Griggs@uk.ibm.com>2020-02-06 02:49:28 +0000
commite5437ef355c6956df7eeb47cf0612867392376a7 (patch)
tree8b75aa12233fe9f2d95579ea32dbd90c547aea3a
parent168dd92537a1171b1a0b67c62ac91f60e8f409db (diff)
downloadnode-new-e5437ef355c6956df7eeb47cf0612867392376a7.tar.gz
module: fix require in node repl
Fixes: https://github.com/nodejs/node/issues/30808 PR-URL: https://github.com/nodejs/node/pull/30835 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
-rw-r--r--lib/internal/modules/cjs/loader.js6
-rw-r--r--test/parallel/test-repl-require.js81
-rw-r--r--test/parallel/test-require-resolve.js27
3 files changed, 87 insertions, 27 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index ffeaf2873e..c1142b819b 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -810,11 +810,11 @@ Module._resolveLookupPaths = function(request, parent) {
return paths.length > 0 ? paths : null;
}
- // With --eval, parent.id is not set and parent.filename is null.
+ // In REPL, parent.filename is null.
if (!parent || !parent.id || !parent.filename) {
// Make require('./path/to/foo') work - normally the path is taken
- // from realpath(__filename) but with eval there is no filename
- const mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
+ // from realpath(__filename) but in REPL there is no filename
+ const mainPaths = ['.'];
debug('looking for %j in %j', request, mainPaths);
return mainPaths;
diff --git a/test/parallel/test-repl-require.js b/test/parallel/test-repl-require.js
index 818fca7431..391b629b1b 100644
--- a/test/parallel/test-repl-require.js
+++ b/test/parallel/test-repl-require.js
@@ -11,28 +11,61 @@ if (!common.isMainThread)
process.chdir(fixtures.fixturesDir);
const repl = require('repl');
-const server = net.createServer((conn) => {
- repl.start('', conn).on('exit', () => {
- conn.destroy();
- server.close();
+{
+ const server = net.createServer((conn) => {
+ repl.start('', conn).on('exit', () => {
+ conn.destroy();
+ server.close();
+ });
});
-});
-
-const host = common.localhostIPv4;
-const port = 0;
-const options = { host, port };
-
-let answer = '';
-server.listen(options, function() {
- options.port = this.address().port;
- const conn = net.connect(options);
- conn.setEncoding('utf8');
- conn.on('data', (data) => answer += data);
- conn.write('require("baz")\nrequire("./baz")\n.exit\n');
-});
-
-process.on('exit', function() {
- assert.strictEqual(/Cannot find module/.test(answer), false);
- assert.strictEqual(/Error/.test(answer), false);
- assert.strictEqual(answer, '\'eye catcher\'\n\'perhaps I work\'\n');
-});
+
+ const host = common.localhostIPv4;
+ const port = 0;
+ const options = { host, port };
+
+ let answer = '';
+ server.listen(options, function() {
+ options.port = this.address().port;
+ const conn = net.connect(options);
+ conn.setEncoding('utf8');
+ conn.on('data', (data) => answer += data);
+ conn.write('require("baz")\nrequire("./baz")\n.exit\n');
+ });
+
+ process.on('exit', function() {
+ assert.strictEqual(/Cannot find module/.test(answer), false);
+ assert.strictEqual(/Error/.test(answer), false);
+ assert.strictEqual(answer, '\'eye catcher\'\n\'perhaps I work\'\n');
+ });
+}
+
+// Test for https://github.com/nodejs/node/issues/30808
+// In REPL, we shouldn't look up relative modules from 'node_modules'.
+{
+ const server = net.createServer((conn) => {
+ repl.start('', conn).on('exit', () => {
+ conn.destroy();
+ server.close();
+ });
+ });
+
+ const host = common.localhostIPv4;
+ const port = 0;
+ const options = { host, port };
+
+ let answer = '';
+ server.listen(options, function() {
+ options.port = this.address().port;
+ const conn = net.connect(options);
+ conn.setEncoding('utf8');
+ conn.on('data', (data) => answer += data);
+ conn.write('require("./bar")\n.exit\n');
+ });
+
+ process.on('exit', function() {
+ assert.strictEqual(/Uncaught Error: Cannot find module '\.\/bar'/.test(answer), true);
+
+ assert.strictEqual(/code: 'MODULE_NOT_FOUND'/.test(answer), true);
+ assert.strictEqual(/requireStack: \[ '<repl>' \]/.test(answer), true);
+ });
+}
diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js
index 2ffcc71185..484263c226 100644
--- a/test/parallel/test-require-resolve.js
+++ b/test/parallel/test-require-resolve.js
@@ -23,6 +23,8 @@
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
+const { builtinModules } = require('module');
+const path = require('path');
assert.strictEqual(
require.resolve(fixtures.path('a')).toLowerCase(),
@@ -52,3 +54,28 @@ const re = /^The "request" argument must be of type string\. Received type \w+$/
message: re
});
});
+
+// Test require.resolve.paths.
+{
+ // builtinModules.
+ builtinModules.forEach((mod) => {
+ assert.strictEqual(require.resolve.paths(mod), null);
+ });
+
+ // node_modules.
+ const resolvedPaths = require.resolve.paths('eslint');
+ assert.strictEqual(Array.isArray(resolvedPaths), true);
+ assert.strictEqual(resolvedPaths[0].includes('node_modules'), true);
+
+ // relativeModules.
+ const relativeModules = ['.', '..', './foo', '../bar'];
+ relativeModules.forEach((mod) => {
+ const resolvedPaths = require.resolve.paths(mod);
+ assert.strictEqual(Array.isArray(resolvedPaths), true);
+ assert.strictEqual(resolvedPaths.length, 1);
+ assert.strictEqual(resolvedPaths[0], path.dirname(__filename));
+
+ // Shouldn't look up relative modules from 'node_modules'.
+ assert.strictEqual(resolvedPaths.includes('/node_modules'), false);
+ });
+}