In the Electron project, every time you modify the code, you need to manually close the application and then execute itnpm start
Restart the app.
Nodemon is a very practical tool that is mainly used to automatically monitor file changes and restart the server when developing applications.
Install nodemon
Install nodemon in the development environment:
npm i nodemon -D
Check whether the installation is successful:
nodemon -v
ReviseOrder:
"scripts": { "start": "nodemon --exec electron ." },
--exec
Parameter tellnodemon
The command to be executed. This is specifiedelectron .
。
ReviseAfter that, the application will restart automatically.
But modify.html
Page, the application does not automatically refresh the page.
Configure nodemon
nodemonThere are three configuration methods: command parameters,and
。
Configuration priority: >
> Command parameters.
Create in the root directoryFile and configuration
rule:
{ "ignore": [ "node_modules", "dist" ], "restartable": "r", "watch": ["*.*"], "ext": "html,js,css" }
existMedium configuration
{ "nodemonConfig": { "ignore": [ "node_modules", "dist" ], "restartable": "r", "watch": ["*.*"], "ext": "html,js,css" }, }
Configure via command line
use--watch
Parameters can specify the file or directory to monitor:
nodemon --watch./src --watch./config
Monitoring is specified here./src
and./config
File changes in the directory.
use--ignore
Parameters can ignore certain files or directories:
nodemon --ignore./node_modules --ignore./dist
This will be ignored./node_modules
and./dist
File changes in the directory.
Generally speaking, if you want to use nodemon to monitor and automatically restart Electron projects, you should make sure that the file parameters in the command are the correct project.Entry file, usually。
nodemon configuration items
-
watch
: Monitor the path to a file or folder.- : When files under these paths change, nodemon triggers the corresponding action (usually restarting the application).
- For example:
"watch": ["*.*"]
-
ignore
: Ignore the path to monitor.- Used to exclude files or folders that change frequently but should not trigger the application to restart.
- For example:
"ignore": [ "node_modules", "dist" ]
-
delay
: Set the delay time in milliseconds.- When the file changes, nodemon does not restart the application immediately, but waits for the specified delay time and will trigger a restart if no more file changes.
- For example:
"delay": 2000
-
ext
: Specify the default file extension.- If this option is not specified, nodemon will monitor by default
.js
document. Multiple extensions can be separated by commas. - For example:
"ext": "js,json,html"
- If this option is not specified, nodemon will monitor by default
-
script
: Specify the monitored file (usually at the project entrance.js
document).- When this file changes, nodemon triggers a restart.
- For example:
"script": "",
-
exec
: The command to be executed.- When the monitored file changes, nodemon executes this command to restart the application.
- For example:
"start": "nodemon --exec electron ."
-
restartable
: Configure the short command to restart the application.- For example:
"restartable": "r"
, you can enter it in the vs code terminalr
, press Enter, restart the application.
- For example:
This is the article about the automatic restart of Electron using Nodemon. This is all about this article. For more related content on Electron using Nodemon, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!