| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -813,11 +813,11 @@ Module._resolveLookupPaths = function(request, parent) { | |||
| 813 | 813 | return paths.length > 0 ? paths : null; | |
| 814 | 814 | } | |
| 815 | 815 | ||
| 816 | - // With --eval, parent.id is not set and parent.filename is null. | ||
| 816 | + // In REPL, parent.filename is null. | ||
| 817 | 817 | if (!parent || !parent.id || !parent.filename) { | |
| 818 | 818 | // Make require('./path/to/foo') work - normally the path is taken | |
| 819 | - // from realpath(__filename) but with eval there is no filename | ||
| 820 | - const mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths); | ||
| 819 | + // from realpath(__filename) but in REPL there is no filename | ||
| 820 | + const mainPaths = ['.']; | ||
| 821 | 821 | ||
| 822 | 822 | debug('looking for %j in %j', request, mainPaths); | |
| 823 | 823 | return mainPaths; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,28 +11,61 @@ if (!common.isMainThread) | |||
| 11 | 11 | process.chdir(fixtures.fixturesDir); | |
| 12 | 12 | const repl = require('repl'); | |
| 13 | 13 | ||
| 14 | - const server = net.createServer((conn) => { | ||
| 15 | - repl.start('', conn).on('exit', () => { | ||
| 16 | - conn.destroy(); | ||
| 17 | - server.close(); | ||
| 14 | + { | ||
| 15 | + const server = net.createServer((conn) => { | ||
| 16 | + repl.start('', conn).on('exit', () => { | ||
| 17 | + conn.destroy(); | ||
| 18 | + server.close(); | ||
| 19 | + }); | ||
| 18 | 20 | }); | |
| 19 | - }); | ||
| 20 | - | ||
| 21 | - const host = common.localhostIPv4; | ||
| 22 | - const port = 0; | ||
| 23 | - const options = { host, port }; | ||
| 24 | - | ||
| 25 | - let answer = ''; | ||
| 26 | - server.listen(options, function() { | ||
| 27 | - options.port = this.address().port; | ||
| 28 | - const conn = net.connect(options); | ||
| 29 | - conn.setEncoding('utf8'); | ||
| 30 | - conn.on('data', (data) => answer += data); | ||
| 31 | - conn.write('require("baz")\nrequire("./baz")\n.exit\n'); | ||
| 32 | - }); | ||
| 33 | - | ||
| 34 | - process.on('exit', function() { | ||
| 35 | - assert.strictEqual(/Cannot find module/.test(answer), false); | ||
| 36 | - assert.strictEqual(/Error/.test(answer), false); | ||
| 37 | - assert.strictEqual(answer, '\'eye catcher\'\n\'perhaps I work\'\n'); | ||
| 38 | - }); | ||
| 21 | + | ||
| 22 | + const host = common.localhostIPv4; | ||
| 23 | + const port = 0; | ||
| 24 | + const options = { host, port }; | ||
| 25 | + | ||
| 26 | + let answer = ''; | ||
| 27 | + server.listen(options, function() { | ||
| 28 | + options.port = this.address().port; | ||
| 29 | + const conn = net.connect(options); | ||
| 30 | + conn.setEncoding('utf8'); | ||
| 31 | + conn.on('data', (data) => answer += data); | ||
| 32 | + conn.write('require("baz")\nrequire("./baz")\n.exit\n'); | ||
| 33 | + }); | ||
| 34 | + | ||
| 35 | + process.on('exit', function() { | ||
| 36 | + assert.strictEqual(/Cannot find module/.test(answer), false); | ||
| 37 | + assert.strictEqual(/Error/.test(answer), false); | ||
| 38 | + assert.strictEqual(answer, '\'eye catcher\'\n\'perhaps I work\'\n'); | ||
| 39 | + }); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + // Test for https://github.com/nodejs/node/issues/30808 | ||
| 43 | + // In REPL, we shouldn't look up relative modules from 'node_modules'. | ||
| 44 | + { | ||
| 45 | + const server = net.createServer((conn) => { | ||
| 46 | + repl.start('', conn).on('exit', () => { | ||
| 47 | + conn.destroy(); | ||
| 48 | + server.close(); | ||
| 49 | + }); | ||
| 50 | + }); | ||
| 51 | + | ||
| 52 | + const host = common.localhostIPv4; | ||
| 53 | + const port = 0; | ||
| 54 | + const options = { host, port }; | ||
| 55 | + | ||
| 56 | + let answer = ''; | ||
| 57 | + server.listen(options, function() { | ||
| 58 | + options.port = this.address().port; | ||
| 59 | + const conn = net.connect(options); | ||
| 60 | + conn.setEncoding('utf8'); | ||
| 61 | + conn.on('data', (data) => answer += data); | ||
| 62 | + conn.write('require("./bar")\n.exit\n'); | ||
| 63 | + }); | ||
| 64 | + | ||
| 65 | + process.on('exit', function() { | ||
| 66 | + assert.strictEqual(/Uncaught Error: Cannot find module '\.\/bar'/.test(answer), true); | ||
| 67 | + | ||
| 68 | + assert.strictEqual(/code: 'MODULE_NOT_FOUND'/.test(answer), true); | ||
| 69 | + assert.strictEqual(/requireStack: \[ '<repl>' \]/.test(answer), true); | ||
| 70 | + }); | ||
| 71 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,8 @@ | |||
| 23 | 23 | const common = require('../common'); | |
| 24 | 24 | const fixtures = require('../common/fixtures'); | |
| 25 | 25 | const assert = require('assert'); | |
| 26 | + const { builtinModules } = require('module'); | ||
| 27 | + const path = require('path'); | ||
| 26 | 28 | ||
| 27 | 29 | assert.strictEqual( | |
| 28 | 30 | require.resolve(fixtures.path('a')).toLowerCase(), | |
@@ -52,3 +54,28 @@ const re = /^The "request" argument must be of type string\. Received type \w+$/ | |||
| 52 | 54 | message: re | |
| 53 | 55 | }); | |
| 54 | 56 | }); | |
| 57 | + | ||
| 58 | + // Test require.resolve.paths. | ||
| 59 | + { | ||
| 60 | + // builtinModules. | ||
| 61 | + builtinModules.forEach((mod) => { | ||
| 62 | + assert.strictEqual(require.resolve.paths(mod), null); | ||
| 63 | + }); | ||
| 64 | + | ||
| 65 | + // node_modules. | ||
| 66 | + const resolvedPaths = require.resolve.paths('eslint'); | ||
| 67 | + assert.strictEqual(Array.isArray(resolvedPaths), true); | ||
| 68 | + assert.strictEqual(resolvedPaths[0].includes('node_modules'), true); | ||
| 69 | + | ||
| 70 | + // relativeModules. | ||
| 71 | + const relativeModules = ['.', '..', './foo', '../bar']; | ||
| 72 | + relativeModules.forEach((mod) => { | ||
| 73 | + const resolvedPaths = require.resolve.paths(mod); | ||
| 74 | + assert.strictEqual(Array.isArray(resolvedPaths), true); | ||
| 75 | + assert.strictEqual(resolvedPaths.length, 1); | ||
| 76 | + assert.strictEqual(resolvedPaths[0], path.dirname(__filename)); | ||
| 77 | + | ||
| 78 | + // Shouldn't look up relative modules from 'node_modules'. | ||
| 79 | + assert.strictEqual(resolvedPaths.includes('/node_modules'), false); | ||
| 80 | + }); | ||
| 81 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments