Skip to main content

Hello World (first programs) · Beginner · 12 min

Hello World in Deno: TypeScript out of the box and permissions

By Padding · Updated 18 Nov 2026
denoland/deno:2.9.8 image · Deno 2.9.8

You will install Deno 2.9, run a TypeScript Hello World with no config, watch Deno refuse a file read until you grant permission, then serve a hello page and compile it into a single executable.

On this page

Deno is a runtime for JavaScript and TypeScript, built on the same V8 engine as Chrome and Node.js. Two things set it apart from Node.js on day one. It runs .ts files directly, with no tsconfig.json, no package.json and no build step. And a script starts with no access to your files, network or environment variables: you grant each one on the command line, and Deno refuses anything you did not grant.

Deno also ships its own formatter, linter, test runner and compiler inside the one deno binary, and it prefers web-standard APIs: the fetch, Request and Response you would use in a browser are the same objects on the server. In this tutorial you install Deno 2.9.8, run Hello World, trip over the permission model on purpose, then build a small web server, test it and compile it into one file you can copy to another machine.

Deno is used for command-line scripts, small HTTP APIs and edge functions. Supabase Edge Functions and Netlify Edge Functions both run your code on Deno, so the permissions and web APIs you meet here are the ones those platforms use. Deno can also load npm packages with an npm: prefix, such as import express from "npm:express@5", so you are not cut off from the npm registry.

Step 1 · Install Deno

Pick the line for your system. On macOS or Linux, the official install script downloads one binary into ~/.deno/bin. On Ubuntu it needs curl and unzip first:

Bash
sudo apt install -y curl unzip
curl -fsSL https://deno.land/install.sh | sh -s v2.9.8

On macOS with Homebrew:

Bash
brew install deno

On Windows, in PowerShell:

PowerShell
winget install --id DenoLand.Deno

The sh -s v2.9.8 part passes a version to the install script, so you get the exact release this page was written against. Leave it off to get the latest. The script may ask whether to add Deno to your PATH; answer yes, then open a new terminal so the change takes effect.

A binary installed by the script gets no updates from apt. Upgrade it yourself with deno upgrade (or deno upgrade 2.9.8 for a specific release). Homebrew and winget installs upgrade through brew upgrade deno and winget upgrade DenoLand.Deno.

The rest of this tutorial uses bash commands: heredocs such as cat > hello.ts <<'TS', printf, ~/hello-deno, ls and rm -rf. On Windows, run them in WSL or Git Bash, or create each file in your editor with the contents shown and run only the deno lines in PowerShell.

Now confirm the install:

Bash
deno --version

Check it worked

deno --version prints three lines, something like deno 2.9.8 (stable, release, x86_64-unknown-linux-gnu), then the V8 version and the TypeScript version bundled inside Deno. There is one program to keep up to date, not a runtime plus a compiler plus a package manager.

Step 2 · Write and run hello.ts

Make a throwaway folder and write a two-line TypeScript file:

Bash
mkdir -p ~/hello-deno
cd ~/hello-deno
cat > hello.ts <<'TS'
const name: string = "world";
console.log(`Hello, ${name}`);
TS
deno run hello.ts

The cat > hello.ts <<'TS' line writes everything up to the closing TS into hello.ts. You can create the file in your editor instead and paste the two lines of code. What they do:

  • const name: string = "world"; creates a constant called name. The : string is a TypeScript annotation saying the value must be text. Plain JavaScript would reject it as a syntax error; Deno accepts it because it understands TypeScript natively.
  • ` console.log(Hello, ${name}); `` prints a line. The backticks make a template string, and `${name} inserts the value of name.
  • deno run hello.ts loads the file, strips the types and runs what is left. Nothing is written to your folder: no .js file, no node_modules.

There is one thing deno run skips: it does not check your types, so it starts fast. Ask for the check separately:

Bash
deno check hello.ts

Check it worked

deno run hello.ts prints Hello, world. deno check hello.ts prints a line such as Check hello.ts and no errors. ls shows only hello.ts.

Step 3 · Read a file and watch Deno refuse

Write a greeting into a text file, then a script that reads it. The greeting mixes Hausa, Swahili and Yoruba, and the accented á in the Yoruba Báwo ni shows that Deno reads text files as UTF-8 by default:

Bash
printf 'Sannu, Jambo, Báwo ni\n' > greeting.txt
cat > read.ts <<'TS'
const text: string = await Deno.readTextFile("greeting.txt");
console.log(`The file says: ${text.trim()}`);
TS
deno run --no-prompt read.ts || echo "deno exited with status $?"

Line by line:

  • const text: string = await Deno.readTextFile("greeting.txt"); reads the whole file as text. Deno.readTextFile is part of the Deno namespace, Deno's own API for files, processes and the network. await waits for the read to finish; Deno allows await at the top level of a file, outside any function.
  • ` console.log(The file says: ${text.trim()}); `` prints the text. `.trim() removes the line break at the end of the file.
  • --no-prompt tells Deno to fail straight away instead of asking you. Flags for Deno go between run and the file name.

Check it worked

The script stops with an error and the status line, something like:

Text
error: Uncaught (in promise) NotCapable: Requires read access to "greeting.txt", run again with the --allow-read flag
deno exited with status 1

Your own script, in your own folder, was not allowed to read a file next to it. That is Deno's security model: code starts with no permissions, so a dependency you did not write cannot quietly read your SSH keys or send data somewhere. Without --no-prompt, an interactive terminal shows a question such as Deno requests read access to "greeting.txt". Allow?, and you can answer for this one run.

Step 4 · Grant only the permission the script needs

Run it again, granting read access to that one file and nothing else:

Bash
deno run --allow-read=greeting.txt read.ts

--allow-read=greeting.txt is an allowlist. --allow-read on its own would allow reading any file on your machine, which works but throws away the protection you just saw. The same pattern applies to the other permissions: --allow-net=example.com, --allow-env=HOME, --allow-write=out/. Deno 2 also has short forms, such as -R for --allow-read and -N for --allow-net.

Check it worked

The script prints The file says: Sannu, Jambo, Báwo ni, with the accented á intact. If you change the file name in read.ts to anything else and run deno run --no-prompt --allow-read=greeting.txt read.ts, the refusal comes back, because only greeting.txt was granted. Without --no-prompt, an interactive terminal asks for read access to the new file instead; answer n to see the same error.

Step 5 · Start a project with deno init and a task

For anything bigger than one file, Deno uses a deno.json file for settings and named commands, called tasks. Create a project folder:

Bash
cd ~/hello-deno
deno init server
cd server
ls

deno init server creates a folder called server with a starter deno.json, main.ts and main_test.ts. The starter files change between Deno releases, so replace all three with the versions below. First the settings file:

Bash
cat > deno.json <<'JSON'
{
  "tasks": {
    "start": "deno run --allow-net=127.0.0.1:8000 main.ts",
    "build": "deno compile --allow-net=127.0.0.1:8000 --output hello-server main.ts"
  }
}
JSON

Each entry under tasks is a name and the command it stands for. deno task start will run the server with network access to one address and port only, and deno task build will compile it. Anyone who opens the project can read the exact permissions it needs in this one file.

Then the server:

Bash
cat > main.ts <<'TS'
export function handler(req: Request): Response {
  const url = new URL(req.url);
  return new Response(`Hello from Deno at ${url.pathname}\n`);
}

if (import.meta.main) {
  Deno.serve({ hostname: "127.0.0.1", port: 8000 }, handler);
}
TS

What each part does:

  • export function handler(req: Request): Response { declares a function that takes a web-standard Request and returns a Response, the same classes a browser uses for fetch. export lets the test file import it.
  • const url = new URL(req.url); parses the requested address so you can read its parts.
  • ` return new Response(Hello from Deno at ${url.pathname}\n); `` sends back plain text that includes the path, such as `/hello.
  • if (import.meta.main) { is true only when this file is the one you ran, not when another file imports it. It stops the tests from starting a server.
  • Deno.serve({ hostname: "127.0.0.1", port: 8000 }, handler); starts an HTTP server on your own machine only, port 8000, and calls handler for every request.

Check it worked

ls after deno init listed deno.json, main.ts and main_test.ts. cat deno.json now shows the two tasks, start and build.

Step 6 · Run the server and check it with curl

Start the server with the task:

Bash
deno task start

Deno prints the task's command and then a line such as Listening on http://127.0.0.1:8000/. Leave it running. Open a second terminal and request a page:

Bash
curl http://127.0.0.1:8000/hello

You can also open http://127.0.0.1:8000/hello in a browser on the same machine. When you are done, go back to the first terminal and press Ctrl+C to stop the server.

Check it worked

curl prints Hello from Deno at /hello. Try another path, such as /lagos, and the reply changes to match. If you edit deno.json to remove --allow-net=127.0.0.1:8000 and start again, Deno stops before the server opens and asks Deno requests net access to "127.0.0.1:8000". Allow?. Answer n and it exits with a Requires net access error, the same way it refused the file read. To get the error without the question, put --no-prompt after deno run in the task for this experiment. Put the task back as it was afterwards.

Step 7 · Format, lint and test with the built-in tools

Write a test for the handler. It calls the function directly with a Request, so no server needs to run:

Bash
cat > main_test.ts <<'TS'
import { handler } from "./main.ts";

Deno.test("handler greets the requested path", async () => {
  const res = handler(new Request("http://localhost/tutorials"));
  const body = await res.text();
  if (body !== "Hello from Deno at /tutorials\n") {
    throw new Error(`unexpected body: ${body}`);
  }
});
TS
deno fmt --check
deno lint
deno test
  • Deno.test("...", async () => { ... }) registers a test with a name. The test fails if the function throws.
  • deno fmt --check reports any file whose formatting differs from Deno's style, without changing it. Run deno fmt with no flag to fix them.
  • deno lint looks for likely bugs, such as unused variables.
  • deno test type-checks and runs every file whose name ends in _test.ts. Unlike deno run, it checks types first.

None of these needed an install. In a Node.js project each one is usually a separate npm package with its own config file.

Check it worked

deno fmt --check and deno lint each print a Checked ... files line and no problems. deno test ends with something like ok | 1 passed | 0 failed. Change /tutorials\n in the test to /tutorial\n and run deno test again to see a failure with the unexpected body message, then change it back.

Step 8 · Compile a single executable

Run the build task:

Bash
deno task build
ls -lh hello-server

deno compile bundles your code together with a copy of the Deno runtime into one file called hello-server (hello-server.exe on Windows). The --allow-net=127.0.0.1:8000 given at compile time is baked in, so the executable keeps the same limits. The first compile downloads the runtime it embeds, and the result is tens of megabytes, because the whole runtime is inside. On prepaid mobile data, compile once and copy the file rather than rebuilding on every machine.

Start it the same way as any program. On macOS and Linux:

Bash
./hello-server

On Windows, in PowerShell:

PowerShell
.\hello-server.exe

Check it worked

ls -lh hello-server shows one executable file of tens of megabytes. ./hello-server (or .\hello-server.exe on Windows) prints the Listening on line, and curl http://127.0.0.1:8000/compiled in a second terminal prints Hello from Deno at /compiled. The machine you copy it to does not need Deno installed, but it must have the same operating system and processor type; use --target to build for another one.

Step 9 · Avoid the common mistakes, then clean up

These catch most people in their first week with Deno:

  • Putting flags after the file name. deno run read.ts --allow-read does not grant anything: everything after the file name goes to your script as Deno.args. Write deno run --allow-read read.ts.
  • Granting everything to make an error go away. -A (allow all) works, and removes the protection the permission model gives you. Grant the narrowest allowlist that works, as in the tasks above.
  • Believing deno run checked your types. It did not. Run deno check, or rely on deno test, which checks before running.
  • Writing require() in a .ts file. Deno uses ES modules: import and export. Packages from npm load through npm: specifiers, not a node_modules folder you install by hand.
  • AddrInUse when starting the server. Another program, often a server you forgot to stop, already has port 8000. Stop it with Ctrl+C in its terminal, or change the port in main.ts and deno.json.

Then delete the tutorial folder, which holds both scripts, the text file, the project and the compiled executable:

Bash
cd ~
rm -rf ~/hello-deno

The runtime downloaded by deno compile stays in Deno's cache, shared with any other Deno projects you have. deno clean empties the whole cache if you want the space back; Deno downloads what it needs again next time.

Check it worked

ls ~/hello-deno now reports No such file or directory, and deno --version still works for the next tutorial.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.