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):

>_
deno run --allow-net main.ts        # network    (-N)
deno run --allow-read main.ts       # filesystem (-R)
deno run --allow-env main.ts        # env vars   (-E)

Scope them down to exactly what's needed, and combine as required:

>_
deno run --allow-read=./data --allow-net=api.example.com main.ts

Use --deny-* to carve out exceptions, or -A / --allow-all to skip the sandbox entirely (handy in trusted environments, but it gives up the guarantees):

>_
deno run -A main.ts

See Permissions for every flag and Security for the model behind them.

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.

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

Did you find what you needed?

Edit this page
Privacy policy