deno task provides a cross platform way to define and execute custom commands
specific to a codebase.
To get started, define your commands in your codebase's
Deno configuration file under a
"data": "deno task collect && deno task analyze",
"collect": "deno run --allow-read=. --allow-write=. scripts/collect.js",
"analyze": "deno run --allow-read=. scripts/analyze.js"
To get an output showing all the defined tasks, run:
Executing a task
To execute a specific task, run:
deno task task-name [additional args]...
In the example above, to run the
data task we would do:
deno task data
Specifying the current working directory
deno task executes commands with the directory of the Deno
configuration file (ex. deno.json) as the current working directory. This
allows tasks to use relative paths and continue to work regardless of where in
the directory tree you happen to execute the deno task from. In some scenarios,
this may not be desired and this behavior can be overridden with the
INIT_CWD will be set with the full path to the directory the task was run in,
if not already set. This aligns with the same behavior as
For example, the following task will change the current working directory of the task to be in the same directory the user ran the task from and then output the current working directory which is now that directory (remember, this works on Windows too because deno task is cross platform).
"my_task": "cd $INIT_CWD && pwd"
deno task was run from
Since tasks are run using the directory of the Deno configuration file as the
current working directory, it may be useful to know the directory the
deno task was executed from instead. This is possible by using the
environment variable in a task or script launched from
deno task (works the
same way as in
npm run, but in a cross platform way).
For example, to provide this directory to a script in a task, do the following (note the directory is surrounded in double quotes to keep it as a single argument in case it contains spaces):
"start": "deno run main.ts \"$INIT_CWD\""
deno task uses a cross platform shell that's a subset of sh/bash to execute
Boolean lists provide a way to execute additional commands based on the exit
code of the initial command. They separate commands using the
&& operator provides a way to execute a command and if it succeeds (has
an exit code of
0) it will execute the next command:
deno run --allow-read=. --allow-write=. collect.ts && deno run --allow-read=. analyze.ts
|| operator is the opposite. It provides a way to execute a command and
only if it fails (has a non-zero exit code) it will execute the next command:
deno run --allow-read=. --allow-write=. collect.ts || deno run play_sad_music.ts
Sequential lists are similar to boolean lists, but execute regardless of whether
the previous command in the list passed or failed. Commands are separated with a
deno run output_data.ts ; deno run --allow-net server.ts
Async commands provide a way to make a command execute asynchronously. This can
be useful when starting multiple processes. To make a command asynchronous, add
& to the end of it. For example the following would execute
sleep 1 && deno run --allow-net client.ts and
deno run --allow-net server.ts
at the same time:
sleep 1 && deno run --allow-net client.ts & deno run --allow-net server.ts
Unlike in most shells, the first async command to fail will cause all the other
commands to fail immediately. In the example above, this would mean that if the
client command fails then the server command will also fail and exit. You can
opt out of this behavior by adding
|| true to the end of a command, which will
0 exit code. For example:
deno run --allow-net client.ts || true & deno run --allow-net server.ts || true
Environment variables are defined like the following:
Here's an example of using one in a task with shell variable substitution and then with it being exported as part of the environment of the spawned Deno process (note that in the JSON configuration file the double quotes would need to be escaped with backslashes):
export VAR=hello && echo $VAR && deno eval "console.log('Deno: ' + Deno.env.get('VAR'))"
Setting environment variables for a command
To specify environment variable(s) before a command, list them like so:
VAR=hello VAR2=bye deno run main.ts
This will use those environment variables specifically for the following command.
Shell variables are similar to environment variables, but won't be exported to spawned commands. They are defined with the following syntax:
If we use a shell variable instead of an environment variable in a similar example to what's shown in the previous "Environment variables" section:
VAR=hello && echo $VAR && deno eval "console.log('Deno: ' + Deno.env.get('VAR'))"
We will get the following output:
Shell variables can be useful when we want to re-use a value, but don't want it available in any spawned processes.
Pipelines provide a way to pipe the output of one command to another.
The following command pipes the stdout output "Hello" to the stdin of the spawned Deno process:
echo Hello | deno run main.ts
To pipe stdout and stderr, use
deno eval 'console.log(1); console.error(2);' |& deno run main.ts
$(command) syntax provides a way to use the output of a command in other
commands that get executed.
For example, to provide the output of getting the latest git revision to another command you could do the following:
deno run main.ts $(git rev-parse HEAD)
Another example using a shell variable:
REV=$(git rev-parse HEAD) && deno run main.ts $REV && echo $REV
Negate exit code
To negate the exit code, add an exclamation point and space before a command:
# change the exit code from 1 to 0
! deno eval 'Deno.exit(1);'
Redirects provide a way to pipe stdout and/or stderr to a file.
For example, the following redirects stdout of
deno run main.ts to a file
file.txt on the file system:
deno run main.ts > file.txt
To instead redirect stderr, use
deno run main.ts 2> file.txt
To redirect both stdout and stderr, use
deno run main.ts &> file.txt
To append to a file, instead of overwriting an existing one, use two right angle brackets instead of one:
deno run main.ts >> file.txt
Suppressing either stdout, stderr, or both of a command is possible by
/dev/null. This works in a cross platform way including on
# suppress stdout
deno run main.ts > /dev/null
# suppress stderr
deno run main.ts 2> /dev/null
# suppress both stdout and stderr
deno run main.ts &> /dev/null
Note that redirecting input and multiple redirects are currently not supported.
Glob expansion is supported in Deno 1.34 and above. This allows for specifying globs to match files in a cross platform way.
# match .ts files in the current and descendant directories
# match .ts files in the current directory
# match files that start with "data", have a single number, then end with .csv
The supported glob characters are
deno task ships with several built-in commands that work the same out of the
box on Windows, Mac, and Linux.
cp- Copies files.
mv- Moves files.
rm- Remove files or directories.
rm -rf [FILE]...- Commonly used to recursively delete files or directories.
mkdir- Makes directories.
mkdir -p DIRECTORY...- Commonly used to make a directory and all its parents with no error if it exists.
pwd- Prints the name of the current/working directory.
sleep- Delays for a specified amount of time.
sleep 1to sleep for 1 second,
sleep 0.5to sleep for half a second, or
sleep 1mto sleep a minute
echo- Displays a line of text.
cat- Concatenates files and outputs them on stdout. When no arguments are provided it reads and outputs stdin.
exit- Causes the shell to exit.
unset- Unsets environment variables.
xargs- Builds arguments from stdin and executes a command.
Note that if you wish to execute any of these commands in a non-cross platform
way on Mac or Linux, then you may do so by running it through
sh -c <command> (ex.
sh -c cp source destination).
deno task falls back to reading from the
"scripts" entries in a package.json
file if it is discovered. Note that Deno does not respect or support any npm
life cycle events like
postinstall—you must explicitly run the
script entries you want to run (ex.
deno cache main.ts && deno task postinstall).