This tutorial shows you how to enable auto restart in Deno every time file changes detected using Denon.
If you ever use Node.js, you may be familiar with nodemon, which is used for auto-restarting the application every time file changes in the directory are detected. That can be helpful for development because you don't need to perform manual restart every time you make a change in a file. For Deno, there is a similar tool called Denon.
Using Denon
First, install denon on your computer.
deno install -qAf --unstable https://deno.land/x/denon@2.4.4/denon.ts
If Denon is successfully installed, you should be able to run denon
command.
To run a script, use denon run
command.
denon run app.ts
It's also possible to pass flags for deno, or flags for the application like the below example.
denon run --allow-net app.ts --app-arguments
If you make any change to the source code, the server should be restarted (you can see the logs in the terminal).
Creating Configuration
Instead of running the same command again and again, it's better to put the command in a config. Denon has support to use a configuration file named scripts.json
. In the configuration file, you can set the scripts that can be run and the settings for the watcher. To create the configuration file, run the below command.
denon --init
The result of the above command is a file named scripts.json that contains a basic configuration.
{
"$schema": "https://deno.land/x/denon/schema.json",
"scripts": {
"start": {
"cmd": "deno run app.ts",
"desc": "run my app.ts file"
}
}
}
You may need to edit the auto-generated configuration file. The file uses json format. It contains the list of scripts that can be run and also the settings for the watcher. Each script is identified by a unique name. For each script, you can define what command to run as cmd
field. Another field you can add is allow
which makes it possible to pass the permissions required to run the application.
Besides scripts
, you can also add watcher
configuration which may contain the following fields.
interval?: number
: Time between retries.exts?: string[]
: The extensions of the files that will be scanned to detect changes.match?: string[]
: The locations where the scanning will be performed.skip?: string[]
: List of files that will not be scanned.legacy?: boolean
: Whether to use the legacy file monitoring algorithm. (walking).
{
"$schema": "https://deno.land/x/denon/schema.json",
"scripts": {
"start": {
"cmd": "deno run app.ts",
"desc": "Run the main server.",
"allow": [
"net", "read"
]
}
},
"watcher": {
"interval": 500,
"exts": ["js", "jsx", "ts", "tsx", "json"],
"match": ["**/*.*"],
"skip": ["*/.git/*"],
"legacy": false
}
}
For the allow
field, you can pass an array like the above example. Alternatively, you can pass an object.
"allow": {
"read": "/etc,/tmp",
"net": true
},
Having added your script to the scripts
section of the configuration file, you can use denon {script_name}
command to run it using Denon. Below is the example to run the start
script defined above.
denon start
Summary
That's it for this tutorial. You can use Denon if you want to have auto-restart enabled during development.