• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5const assert = require('assert');
6const net = require('net');
7
8if (!common.isMainThread)
9  common.skip('process.chdir is not available in Workers');
10
11process.chdir(fixtures.fixturesDir);
12const repl = require('repl');
13
14{
15  const server = net.createServer((conn) => {
16    repl.start('', conn).on('exit', () => {
17      conn.destroy();
18      server.close();
19    });
20  });
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}
72