Search code examples
javascriptexpressbabeljsnodemon

When using babel with nodemon, view is not updated when files are changed?


package.json

  "scripts": {
    "build": "babel app.js -d dist",
    "start": "npm run build && nodemon dist/app.js"
  },

...

  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "nodemon": "^1.18.9"
  }

app.js

import express from 'express';
const app = express();
const port = process.env.PORT || 3000;

app.use('/', (req, res) => {
  res.send('World 1');
});

app.listen(port, () => {
  console.log('Example app listening on port 3000!');
});

When I run npm start

enter image description here

Browser output

enter image description here

When changes are made to the code, app restarts.

Here I made 'World 1' to 'World 2'

enter image description here

But when I refresh the browser it still shows World 1

If I do npm start again and rebuild, then the browser shows 'World 2'

How do I fix this?

[example-node-server]4


Solution

  • "start": "nodemon dist/app.js --exec babel-node --presets babel-preset-env",
    

    For babel to work with nodemon you start script should be as above.

    Please go through this.