Skip to main content
On this page

Run code

Deno runs JavaScript and TypeScript directly (no build step, no config) behind a security sandbox that grants access only when you ask for it. This page covers how your code actually runs: permissions, the ways to launch it, watch mode, and tasks.

Run a file Jump to heading

Point Deno at a file. TypeScript runs as-is:

>_
deno run main.ts

deno run is explicit, but you can drop run for a file or task and Deno will figure it out:

>_
deno main.ts          # same as `deno run main.ts`

Permissions: secure by default Jump to heading

This is the part that's different from Node. Code runs in a sandbox with no access to the network, filesystem, environment, or subprocesses until you grant it. A script that tries to read a file without permission stops and asks, or fails if prompts are disabled.

Grant access with --allow-* flags (each has a short form), and scope them down to exactly what's needed:

>_
deno run --allow-net main.ts                     # network    (-N)
deno run --allow-read=./data main.ts             # filesystem (-R), scoped
deno run -N=api.example.com -E main.ts           # combined, short forms

Use --deny-* to carve out exceptions, or -A / --allow-all to skip the sandbox entirely. That is handy in trusted environments, but it gives up the guarantees. See Permissions for every flag and Security for the model behind them.

Passing script arguments Jump to heading

Arguments for your own script go after the script name; Deno passes them through in Deno.args:

main.ts
console.log(Deno.args);
$ deno run main.ts arg1 arg2 arg3
[ "arg1", "arg2", "arg3" ]

For anything beyond a flat list, parse the arguments with parseArgs from jsr:@std/cli or parseArgs from node:util.

Argument and flag ordering Jump to heading

Caution

Anything passed after the script name is a script argument, not a Deno runtime flag. This is a common source of confusion, so make sure runtime flags appear before the script name.

This leads to the following pitfall:

# 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

Run from a URL or stdin Jump to heading

Deno can run code straight from a URL (handy for one-off tools and installers) or from stdin:

>_
deno run https://example.com/script.ts
echo 'console.log(1 + 1)' | deno run -

Remote code is sandboxed like everything else: it gets no permissions unless you grant them.

Reload on change with watch mode Jump to heading

Add --watch and Deno reruns your program whenever a file it depends on changes. No nodemon, no extra dependency:

>_
deno run --watch main.ts

deno test, deno fmt, and others accept --watch too. For what gets watched, excluding paths, and hot module replacement, see Watch mode and HMR.

Run project tasks Jump to heading

Define repeatable commands in deno.json and run them with deno task, the equivalent of npm run:

deno.json
{
  "tasks": {
    "dev": "deno run --watch --allow-net main.ts",
    "start": "deno run --allow-net main.ts"
  }
}
>_
deno task dev

Tasks can run other tasks, set environment variables, and work cross-platform.

Try code quickly Jump to heading

For experiments, evaluate an expression inline or open a REPL:

>_
deno eval "console.log(Deno.version)"
deno repl

Going further Jump to heading

Last updated on

Did you find what you needed?

Edit this page
Privacy policy