Express.js: How to Change the Default Port

By default Express uses port 3000, but sometimes this port is already been used by another application, so you can either close the other application or change the port. In this short tutorial I’m going to show you how easy it is to change the port.

First let’s open the bin/www file and locate this code:

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

Here is where we can configure which ports to use, all you have to do is to replace the number 3000 by another port number. After that your application will not be using port 3000 anymore.

Alternatively you can also configure the port on your app.js:

var app = express();
app.listen(4000);

As you can see I’ve just added the app.listen(4000) line, this will make the application also listen on port 4000, but the difference is that this method won’t make your app stop using port 3000.

It’s up to you to decide which method works better for you, If you have any doubts please let me know in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Obs: Use the tag <pre lang="LANGUAGE"> to include code blocks to your comment.
Example: <pre lang="javascript"> console.log('Test'); </pre>