On this page
Command line interface
Deno is a command line program. The Deno command line interface (CLI) can beused to run scripts, manage dependencies, and even compile your code intostandalone executables. You may be familiar with some simple commands havingfollowed the examples thus far. This page will provide a more detailed overviewof the Deno CLI.
The Deno CLI has a number of subcommands (likerun
,init
andtest
, etc.).They are used to perform different tasks within the Deno runtime environment.Each subcommand has its own set of flags and options (eg --version) that can beused to customize its behavior.
You can view all of the available commands and flags by running thedeno help
subcommand in your terminal, or using the-h
or--help
flags.
Check out theCLI reference guide for a furtherdocumentation on all the subcommands and flags available. We'll take a look at afew commands in a bit more detail below to see how they can be used andconfigured.
An example subcommand -deno run
Jump to heading
You can run a local TypeScript or JavaScript file by specifying its pathrelative to the current working directory:
deno run main.ts
Deno supports running scripts directly from URLs. This is particularly usefulfor quickly testing or running code without downloading it first:
deno run https://docs.deno.com/examples/scripts/hello_world.ts
You can also run a script by piping it through standard input. This is usefulfor integrating with other command-line tools or dynamically generating scripts:
cat main.ts| deno run -
Passing script argumentsJump to heading
Script arguments are additional parameters you can pass to your script whenrunning it from the command line. These arguments can be used to customize thebehavior of your program based on the input provided at runtime. Argumentsshould be passedafter the script name.
To test this out we can make a script that will log the arguments passed to it:
console.log(Deno.args);
When we run that script and pass it some arguments it will log them to theconsole:
$ deno run main.ts arg1 arg2 arg3["arg1","arg2","arg3"]
Argument and flag orderingJump to heading
Note that anything passed after the script name will be passed as a scriptargument and not consumed as a Deno runtime flag. This leads to the followingpitfall:
# Good. We grant net permission to net_client.ts.deno run --allow-net net_client.ts# Bad! --allow-net was passed to Deno.args, throws a net permission error.deno run net_client.ts --allow-net
Common flagsJump to heading
Some flags can be used with multiple related subcommands. We discuss thesebelow.
Watch modeJump to heading
You can supply the--watch
flag todeno run
,deno test
, anddeno fmt
toenable the built-in file watcher. The watcher enables automatic reloading ofyour application whenever changes are detected in the source files. This isparticularly useful during development, as it allows you to see the effects ofyour changes immediately without manually restarting the application.
The files that are watched will depend on the subcommand used:
- for
deno run
anddeno test
the entrypoint, and all local files that theentrypoint statically imports will be watched. - for
deno fmt
all local files and directories specified as command linearguments (or the working directory if no specific files/directories ispassed) are watched.
deno run--watch main.tsdenotest--watchdenofmt--watch
You can exclude paths or patterns from watching by providing the--watch-exclude
flag. The syntax is--watch-exclude=path1,path2
. Forexample:
deno run--watch --watch-exclude=file1.ts,file2.ts main.ts
This will exclude file1.ts and file2.ts from being watched.
To exclude a pattern, remember to surround it in quotes to prevent your shellfrom expanding the glob:
deno run--watch --watch-exclude='*.js' main.ts
Hot Module Replacement modeJump to heading
You can use--watch-hmr
flag withdeno run
to enable the hot modulereplacement mode. Instead of restarting the program, the runtime will try toupdate the program in-place. If updating in-place fails, the program will stillbe restarted.
deno run --watch-hmr main.ts
When a hot module replacement is triggered, the runtime will dispatch aCustomEvent
of typehmr
that will includepath
property in itsdetail
object. You can listen for this event and perform any additional logic that youneed to do when a module is updated (eg. notify a browser over a WebSocketconnection).
addEventListener("hmr",(e)=>{console.log("HMR triggered", e.detail.path);});
Integrity flags (lock files)Jump to heading
Affect commands which can download resources to the cache:deno install
,deno run
,deno test
,deno doc
, anddeno compile
.
--lock<FILE> Check the specified lockfile--frozen[=<BOOLEAN>] Error outif lockfile is out ofdate
Find out more about thesehere.
Cache and compilation flagsJump to heading
Affect commands which can populate the cache:deno install
,deno run
,deno test
,deno doc
, anddeno compile
. As well as the flags above, thisincludes those which affect module resolution, compilation configuration etc.
--config<FILE> Load configurationfile--import-map<FILE> Loadimport mapfile--no-remote Do not resolve remote modules--reload=<CACHE_BLOCKLIST> Reloadsource code cache(recompile TypeScript)--unstable Enable unstable APIs
Runtime flagsJump to heading
Affect commands which execute user code:deno run
anddeno test
. Theseinclude all of the above as well as the following.
Type checking flagsJump to heading
You can type-check your code (without executing it) using the command:
> deno check main.ts
You can also type-check your code before execution by using the--check
argument to deno run:
> deno run--check main.ts
This flag affectsdeno run
anddeno eval
. The following table describes thetype-checking behavior of various subcommands. Here "Local" means that onlyerrors from local code will induce type-errors, modules imported from https URLs(remote) may have type errors that are not reported. (To turn on type-checkingfor all modules, use--check=all
.)
Subcommand | Type checking mode |
---|---|
deno bench | 📁 Local |
deno check | 📁 Local |
deno compile | 📁 Local |
deno eval | ❌ None |
deno repl | ❌ None |
deno run | ❌ None |
deno test | 📁 Local |
Permission flagsJump to heading
These are listedhere.
Other runtime flagsJump to heading
More flags which affect the execution environment.
--cached-only Require that remote dependencies are already cached--inspect=<HOST:PORT> activate inspector on host:port...--inspect-brk=<HOST:PORT> activate inspector on host:port andbreak at...--inspect-wait=<HOST:PORT> activate inspector on host:port andwaitfor...--location<HREF> Value of'globalThis.location' used by some web APIs--prompt Fallback to promptif required permission wasn't passed--seed<NUMBER> Seed Math.random()--v8-flags=<v8-flags> Set V8command line options. For help:...