首先打开终端/命令行然后转到您的项目目录,现在使用命令 npm install nodemon --save-dev 安装 nodemon 这个命令将确保它保存为开发人员依赖项。如果您使用的是 expressjs,那么在您的包文件中它看起来像
{
"name": "expressjs-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}
现在修改 package.json 文件中的“start”值,对于生产我们将使用现有值,但对于开发将使用 nodemon 来跟踪源文件中的更改,而无需重新启动服务器。 start 的新值是 "start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
最终的 package.json 文件看起来像
{
"name": "expressjs-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}
要卸载 nodemon jusy,只需运行命令 npm uninstall nodemon