node.js 如何使用 Nodemon 执行启动脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33879896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute the start script with Nodemon
提问by Citronen
How can I execute the start script from a package.jsonfile with Nodemon?
如何使用 Nodemon从package.json文件中执行启动脚本?
回答by Ashutosh Jha
This will be a simple command for this
这将是一个简单的命令
nodemon --exec npm start
回答by Murat Ozgul
In package json:
在包 json 中:
{
"name": "abc",
"version": "0.0.1",
"description": "my server",
"scripts": {
"start": "nodemon my_file.js"
},
"devDependencies": {
"nodemon": "~1.3.8",
},
"dependencies": {
}
}
Then from the terminal you can use npm start
然后从终端你可以使用 npm start
Nodemon installation: https://www.npmjs.com/package/nodemon
Nodemon 安装:https://www.npmjs.com/package/nodemon
回答by MehranTM
I have a TypeScript file called "server.ts", The following npm scripts configures Nodemon and npm to start my app and monitor for any changes on TypeScript files:
我有一个名为“server.ts”的 TypeScript 文件,以下 npm 脚本配置 Nodemon 和 npm 以启动我的应用程序并监视 TypeScript 文件的任何更改:
"start": "nodemon -e ts --exec \"npm run myapp\"",
"myapp": "tsc -p . && node server.js",
I already have Nodemon on dependencies. When I run npm start, it will ask Nodemon to monitor its files using the -eswitch and then it calls the myappnpm script which is a simple combination of transpiling the typescript files and then starting the resulting server.js. When I change the TypeScript file, because of -eswitch the same cycle happens and new .js files will be generated and executed.
我已经有了 Nodemon 依赖项。当我运行 时npm start,它会要求 Nodemon 使用-e开关来监视它的文件,然后它调用myappnpm 脚本,这是一个简单的组合,即转译打字稿文件,然后启动生成的 server.js。当我更改 TypeScript 文件时,由于-e切换会发生相同的循环,并且会生成并执行新的 .js 文件。
回答by Sukma Saputra
I use Nodemon version 1.88.3 in my Node.js project. To install Nodemon, see in https://www.npmjs.com/package/nodemon.
我在我的 Node.js 项目中使用 Nodemon 版本 1.88.3。要安装 Nodemon,请参阅https://www.npmjs.com/package/nodemon。
Check your package.json, see if "scripts" has changed like this:
检查你的 package.json,看看“脚本”是否像这样改变了:
"scripts": {
"dev": "nodemon server.js"
},
server.jsis my file name, you can use another name for this file like app.js.
server.js是我的文件名,您可以为此文件使用另一个名称,例如app.js.
After that, run this on your terminal: npm run dev
之后,在你的终端上运行: npm run dev
回答by KeatsPeeks
Use -exec:
使用-exec:
"your-script-name": "nodemon [options] --exec 'npm start -s'"
回答by Sohail Ahmad
In package.json file. change file like this
在 package.json 文件中。像这样更改文件
"scripts":{
"start": "node ./bin/www",
"start-dev": "nodemon ./app.js"
},
and then execute npm run start-dev
然后执行npm run start-dev
回答by rkeshri
First change your package.jsonfile,
首先更改您的package.json文件,
"scripts":
{
"start": "node ./bin/www",
"start-dev": "nodemon ./app.js"
},
After that, execute command
之后,执行命令
npm run start-dev
回答by King James Enejo
Nodemon emits events upon every change in state; start, restart crash, etc. You can add a Nodemon configuration file (nodemon.json) like so:
Nodemon 在每次状态变化时发出事件;启动、重启崩溃等。你可以像这样添加一个 Nodemon 配置文件(nodemon.json):
{
"events": {
"start": "npm run *your_file*"
}
}
Read more in Nodemon events — run tasks at server start, restart, crash, exit.
回答by dhahn
You can also install nodemon globally for frequent use:
您还可以全局安装 nodemon 以供频繁使用:
npm i nodemon -gor sudo npm i nodemon -g
npm i nodemon -g或者 sudo npm i nodemon -g
then edit your package.json:
然后编辑你的 package.json:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
Generally, 'dev' specifies developmental use (npm run dev).
通常,'dev' 指定开发用途(npm run dev)。
回答by Lord
It will depend on types of your Nodemon installation. If you install Nodemon globally by using commands (npm install nodemon --globalor npm install nodemon -g), you do not have to specify any script for Nodemon in your package.jsonfile. Just executing command nodemon index.jswill run your project.
这将取决于您的 Nodemon 安装类型。如果使用命令 (npm install nodemon --global或npm install nodemon -g)全局安装 Nodemon ,则不必在package.json文件中为 Nodemon 指定任何脚本。只需执行命令即可nodemon index.js运行您的项目。
But if you install Nodemon locally by command npm install nodemonthen you have to specify the script. If you name it as startthen npm run startor npm startwill trigger the server to run.
但是,如果您通过命令在本地安装 Nodemon,npm install nodemon则必须指定脚本。如果您将其命名为start则npm run start或npm start将触发服务器运行。
// Absolutely no need for global installation
"scripts": {
"start": "nodemon index.js"
}

