SoFunction
Updated on 2025-04-10

Electron uses Nodemon to configure automatic restart method

In the Electron project, every time you modify the code, you need to manually close the application and then execute itnpm startRestart 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 ."
},

--execParameter tellnodemonThe command to be executed. This is specifiedelectron .

ReviseAfter that, the application will restart automatically.

But modify.htmlPage, 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 configurationrule:

{
 "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--watchParameters can specify the file or directory to monitor:

nodemon --watch./src --watch./config 

Monitoring is specified here./srcand./configFile changes in the directory.

use--ignoreParameters can ignore certain files or directories:

nodemon --ignore./node_modules --ignore./dist 

This will be ignored./node_modulesand./distFile 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.jsdocument. Multiple extensions can be separated by commas.
    • For example:"ext": "js,json,html"
  • script: Specify the monitored file (usually at the project entrance.jsdocument).
    • 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.

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!