Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Your First Deno Program

Write and run your first Deno program with deno run, try code in the REPL and deno eval, scaffold a project with deno init, add tasks to deno.json and use watch mode.

Time to run some code. In this lesson you write a small TypeScript file, run it with deno run, experiment in the REPL, create a project with deno init, and give your commands short names with tasks. You need Deno installed (Installing Deno); the commands are shown read-only, and runnable stand-ins let you try the same code in your browser.

Hello, Deno

Create a file called hello.ts:

TypeScript
const name: string = "Deno";
console.log(`Hello, ${name}!`);

Run it:

Terminal
deno run hello.ts
TEXT
Hello, Deno!

That is the whole workflow. There is no tsc, no package.json and no build folder. The run subcommand reads the file, strips the types, and executes the JavaScript. The same line works with a .js file; the extension decides how the file is treated.

The code is plain TypeScript, so you can try it right here. This stand-in runs on the site's TypeScript runner and prints the same text.

TypeScript Hello, typed world

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

What deno run does

When you run deno run hello.ts, Deno does these steps:

  1. Finds the file and every module it imports, and downloads remote ones into the cache the first time.
  2. Strips the types (it does not check them; see TypeScript in Deno).
  3. Starts V8 and runs the file, applying permissions to anything that touches the system.
flowchart LR
  A["deno run hello.ts"] --> B["Resolve and cache imports"]
  B --> C["Strip types"]
  C --> D["V8 runs the code"]
  D --> E["Output and exit code"]

The steps behind one deno run command.

You can also run a program from a URL or standard input:

Terminal
deno run https://example.com/script.ts
echo 'console.log("from stdin")' | deno run -

Running a remote script is exactly why Deno asks for permissions. Only run code from sources you trust, and read it first.

Quick experiments: eval and the REPL

For one-liners use deno eval:

Terminal
deno eval "console.log(2 ** 10)"

For longer experiments start the REPL, an interactive prompt:

Terminal
deno repl
TEXT
> const nums = [1, 2, 3]
undefined
> nums.map((n) => n * 2)
[ 2, 4, 6 ]
> await fetch("https://example.com").then((r) => r.status)

The REPL supports await at the top level, remembers variables between lines, has tab completion and understands TypeScript. Use Ctrl+D or close() to leave.

Reading input and arguments

A program usually needs data from outside. Two quick sources are command-line arguments and the keyboard.

TypeScript
// greet.ts
const who = Deno.args[0] ?? "world";
console.log(`Hello, ${who}!`);
Terminal
deno run greet.ts Ada
TEXT
Hello, Ada!

Deno.args is an array of strings holding everything after the file name. Command-Line Arguments and Environment Variables covers it in depth. To ask a question in the terminal, the global prompt() function (the browser's prompt) works:

TypeScript
const answer = prompt("What is your name?", "stranger");
console.log(`Nice to meet you, ${answer}.`);

Creating a project with deno init

For anything bigger than one file, scaffold a project:

Terminal
deno init my-app
cd my-app

Deno creates a small project:

TEXT
my-app/
  deno.json
  main.ts
  main_test.ts
  • main.ts holds a starter function and a if (import.meta.main) block that runs it.
  • main_test.ts is a first test using Deno.test and @std/assert.
  • deno.json is the optional configuration file with tasks and imports.

The import.meta.main check is worth understanding. It is true only when the file is the entry point you passed to deno run, not when another file imports it. That lets one file act as both a library and a program.

Node.js Library and program in one file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Tasks in deno.json

Typing long commands gets old. A task gives a command a short name. Open deno.json:

JSON
{
  "tasks": {
    "dev": "deno run --watch main.ts",
    "start": "deno run --allow-net=0.0.0.0:8000 main.ts",
    "check": "deno fmt --check && deno lint && deno test"
  }
}

Run a task with deno task:

Terminal
deno task dev
deno task        # lists all tasks

Tasks run in a cross-platform shell that Deno includes, so &&, ||, ;, pipes, environment variable assignment and $VAR expansion work the same on Windows, macOS and Linux. You do not need cross-env or rimraf-style helpers for basic cases.

Watch mode

--watch restarts your program whenever an imported file changes:

Terminal
deno run --watch main.ts

Use it while developing a server or script. The same flag exists on deno test --watch and deno fmt --watch. If you want to tell Deno to watch extra files, such as templates, pass them as --watch=main.ts,views/.

Reading errors

When something breaks, Deno prints the error, its type and a stack trace with file paths and line numbers that point at your TypeScript, not generated code.

TEXT
error: Uncaught (in promise) ReferenceError: x is not defined
    console.log(x);
                ^
    at file:///home/me/hello.ts:1:13

Exit codes follow the usual rules: 0 for success, non-zero for failure. An uncaught error ends the program with code 1, which makes Deno programs easy to chain in shell scripts and CI.

A first useful script

The program below reads a list of numbers and prints a small summary. It uses only standard language features, so it runs the same everywhere.

TypeScript Summarize some numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command runs a TypeScript file in Deno?

    deno run runs TypeScript directly with no separate compile step.
  2. 2. When is import.meta.main true?

    It is true only for the entry module, letting a file work as both library and program.
  3. 3. Where do you define short command names such as dev and start?

    Tasks live in deno.json and are run with deno task <name>.
  4. 4. What does the --watch flag do?

    --watch restarts the process whenever a watched file changes.

Summary

  • deno run file.ts runs JavaScript or TypeScript with no setup.
  • deno eval and deno repl are for quick experiments; the REPL supports top-level await.
  • Deno.args and prompt() bring input into a program.
  • deno init scaffolds main.ts, a test and deno.json.
  • Tasks in deno.json name common commands and run in a portable shell.
  • --watch restarts on file changes.

Next: Deno vs Node.js