VMCO
VMCO

Reputation: 65

NodeJS - npm start every time gets error Port is already in use

I am developing an application with NodeJS and Express and every time I have to npm start I get the error Port is already in use. I have to change the port in www file and npm start again.

I am using Mac BTW. How to solve this issuse?

Is there any good module for live reload?

Upvotes: 1

Views: 1819

Answers (2)

chriopp
chriopp

Reputation: 957

Strange, looks like you are not stopping the server before starting again.

A simple and painless way would be to use nodemon (https://nodemon.io). It acts on changes of your code and restarts the server. You will have to reload the browser yourself.

npm i -g nodemon
nodemon [app|server|yourservername].js 

Live Reload goes a step further, as it triggers the browser to reload the page (using websockets). You may check out supervisor as suggested. I personally prefer lite-server, which requires no configuration to get live reload. Just start it in your applications root directory.

npm i -g lite-server
lite-server 

Upvotes: 2

bc1105
bc1105

Reputation: 1270

Setup a dev script so you don't have to use npm start. This example uses supervisor https://www.npmjs.com/package/supervisor

Create a file called dev.sh

#!/bin/bash

ENV=dev \
  node_modules/supervisor/lib/cli-wrapper.js \
  --watch ., ../core \ # replace this with your paths to watch
  --ignore-symlinks \
  --ignore node_modules,public,client,data \
  --extensions js \
  --quiet \
  --non-interactive \
  --no-restart-on error \
  --instant-kill \
app.js # your server script here

Then when you want to dev just run sh dev. This question deals with killing processes on nix: Find (and kill) process locking port 3000 on Mac

Upvotes: 0

Related Questions