Skip to content

Commit 626c8d0

Browse files
author
Ian McNally
committed
add logging of existing port process on start
1 parent 695f922 commit 626c8d0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Diff for: packages/react-scripts/scripts/start.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ process.env.NODE_ENV = 'development';
1818
require('dotenv').config({silent: true});
1919

2020
var chalk = require('chalk');
21+
var execSync = require('child_process').execSync;
2122
var webpack = require('webpack');
2223
var WebpackDevServer = require('webpack-dev-server');
2324
var historyApiFallback = require('connect-history-api-fallback');
@@ -258,6 +259,17 @@ function run(port) {
258259
runDevServer(host, port, protocol);
259260
}
260261

262+
function getProcessNameOnPort(port) {
263+
var command = 'ps -o command -p "$(lsof -i:' + port + ' -P -t)" | sed -n 2p | tr -d "\n"';
264+
var execOptions = { encoding: 'utf8' };
265+
266+
try {
267+
return execSync(command, execOptions);
268+
} catch(e) {
269+
return null;
270+
}
271+
}
272+
261273
// We attempt to use the default port but if it is busy, we offer the user to
262274
// run on a different port. `detect()` Promise resolves to the next free port.
263275
detect(DEFAULT_PORT).then(port => {
@@ -267,8 +279,11 @@ detect(DEFAULT_PORT).then(port => {
267279
}
268280

269281
clearConsole();
282+
var existingProcess = getProcessNameOnPort(DEFAULT_PORT);
270283
var question =
271-
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.') +
284+
chalk.yellow('Something ' +
285+
((existingProcess) ? '(probably: ' + existingProcess +') ' : '') +
286+
'is already running on port ' + DEFAULT_PORT + '.') +
272287
'\n\nWould you like to run the app on another port instead?';
273288

274289
prompt(question, true).then(shouldChangePort => {

0 commit comments

Comments
 (0)