Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<your_local_git_repo_clone_path>/src/index.js`

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md).

## License

MIT. See [LICENSE](./LICENSE).
MIT. See [LICENSE](./LICENSE).
63 changes: 41 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
17 changes: 16 additions & 1 deletion src/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down