'Configure nodemon to refresh same tab

This could be really rookie. I want to know if there is a way that I can configure nodemon to refresh the same tab instead of opening a new tab each time I make a change in my js files.



Solution 1:[1]

nodemon is not able to do that. What you are looking for is something like browser-sync or LiveReload.js.

Solution 2:[2]

I use a package called reload. Assuming you are doing this on your FE and that you already installed express.js and nodemon first install reload

npm install --save-dev reload

Than create a index.js file or server.js or whatever name your want

this is my index.js:

const express = require('express')
const http = require('http')
const reload = require('reload')
const opn = require('opn')


const app = express()

app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')
app.set('src', './src')
app.use(express.static('src'))

app.get('/', (req, res) => res.render('index'))

const server = http.createServer(app)
server.listen(8080, function() {
  console.log('Listening to port 8080...')
})

opn('http://localhost:8080')

reload(app)

the opn package can be intalled with

npm install opn

it will automatically open your localhost once you type npm start on your terminal

on HTML you have to insert something like this after the closing body tag:

</body>
<script src="/reload/reload.js"></script>

and on package.json the following:

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js -e els,js,html,css,json"

},

The only problem with this is that it will reopen the browser every time you make a change to those files and well as reload the current opened one sure to the opn package. I am still going to create a function to prevent that, eventually. Hope that helps!

Solution 3:[3]

Its Simple. To run your server use npm start instead of nodemon server

Solution 4:[4]

Just run npm start nodemon server. It works for me. Make sure you have latest nodemon version installed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas S.
Solution 2 Patricia Nigro Antlitz
Solution 3 Shuaib Abubakker Bapputty Haji
Solution 4 Mohit Naik