From f8f78654cb7b23ceef4e6d083e0b2cf0b625aafc Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 11 Jun 2026 11:23:24 +0900 Subject: [PATCH 1/2] Added support up to Node.js v24.16.0 --- src/index.js | 63 ++++++++++++++++++++++++++++++++++------------------ src/stub.js | 17 +++++++++++++- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/index.js b/src/index.js index 12ea2b6..57a3f5e 100755 --- a/src/index.js +++ b/src/index.js @@ -438,25 +438,44 @@ Prototype REPL - https://github.com/nodejs/repl } } -const child = spawn(process.execPath, [ - '--inspect-publish-uid=http', - ...process.execArgv, - require.resolve('./stub.js'), - ...process.argv, -], { - cwd: process.cwd(), - windowsHide: true, -}); - -child.stdout.on('data', (data) => { - process.stdout.write(data); -}); - -child.stderr.on('data', (data) => { - const s = data.toString(); - if (s.startsWith('__DEBUGGER_URL__')) { - start(s.split(' ')[1]); - } else if (s !== 'Debugger attached.\n') { - process.stderr.write(data); - } -}); +const net = require('net'); + +function findAvailablePort(startPort = 9229) { + return new Promise((resolve, reject) => { + const server = net.createServer(); + server.listen(startPort, '127.0.0.1', () => { + const port = server.address().port; + server.close(() => resolve(port)); + }); + server.on('error', (err) => { + if (err.code === 'EADDRINUSE') { + resolve(findAvailablePort(startPort + 1)); + } else { + reject(err); + } + }); + }); +} + +findAvailablePort().then(port => { + const child = spawn(process.execPath, [ + `--inspect=${port}`, + `--inspect-publish-uid=http`, + require.resolve('./stub.js'), + ...process.argv, + ], { + cwd: process.cwd(), + windowsHide: true, + }); + child.stdout.on('data', (data) => { + process.stdout.write(data); + }); + child.stderr.on('data', (data) => { + const s = data.toString(); + if (s.startsWith('__DEBUGGER_URL__')) { + start(s.split(' ')[1]); + } else if (s !== 'Debugger attached.\n') { + process.stderr.write(data); + } + }); +}); \ No newline at end of file diff --git a/src/stub.js b/src/stub.js index a7c8866..ceefe84 100644 --- a/src/stub.js +++ b/src/stub.js @@ -5,7 +5,22 @@ const path = require('path'); const inspector = require('inspector'); const util = require('util'); -inspector.open(0, true); +// Handle inspector.open() API differences across Node.js versions +if (!inspector.url()) { + try { + // Try Node.js v24+ API (host, port) + inspector.open(9229, '127.0.0.1'); + } catch (err) { + // Fall back to Node.js v20 and below API (fd, host, port) + try { + inspector.open(0, 'localhost', 9229); + } catch (fallbackErr) { + // If both fail, continue without debugger + console.error('Failed to open inspector:', fallbackErr.message); + } + } +} + process.stderr.write(`__DEBUGGER_URL__ ${inspector.url()}`); if (process.platform !== 'win32') { From 434f27ee47b6907bc2457e6362b6da9f29230acb Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 11 Jun 2026 14:12:41 +0900 Subject: [PATCH 2/2] Updated README.md with troubleshooting steps --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f915ea4..8ac8da3 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,29 @@ If you want to use this REPL by default, you can point `NODE_REPL_EXTERNAL_MODULE` to the result of `which node-prototype-repl`! +#### Troubleshooting + +If the above steps don't work, before running `node-prototype-repl`, +simply clone this repo, navigate to it and run + +```sh +npm install +npm link +``` + +### Using as default Node.js REPL + +Point environment variable `NODE_REPL_EXTERNAL_MODULE` to the result of `which node-prototype-repl` +or, on Windows, `where node-prototype-repl` + +#### Troubleshooting + +If it doesn't work, then point `NODE_REPL_EXTERNAL_MODULE` to `/src/index.js` + ## Contributing See [CONTRIBUTING.md](./CONTRIBUTING.md). ## License -MIT. See [LICENSE](./LICENSE). +MIT. See [LICENSE](./LICENSE). \ No newline at end of file