将babel与nodemon一起使用时,更改文件后视图是否不会更新?

时间:2019-01-07 05:42:56

标签: javascript express babel nodemon

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!');
});

我运行npm start

enter image description here

浏览器输出

enter image description here

更改代码后,应用将重新启动。

在这里,我将“世界1”更改为“世界2”

enter image description here

但是当我刷新浏览器时,它仍然显示World 1

如果我再次执行npm start并进行重建,那么浏览器将显示“世界2”

我该如何解决?

[example-node-server] 4

2 个答案:

答案 0 :(得分:2)

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

要让babel与nodemon一起工作,您启动脚本的步骤应如上所述。

请通过this

答案 1 :(得分:0)

问题在于,nodemon正在运行已构建的代码,而在代码更改后未发生构建。这就是为什么您不更新的原因。

在开发时,您应该考虑运行nodemon而不需要进行构建

nodemon --exec babel-node lib/index.js