Deno

Deno1 is a secure TypeScript and JavaScript runtime built on V8, Rust, and Tokio.

Key Features

  • Secure by default - No file, network, or environment access without explicit permission
  • TypeScript support - Built-in TypeScript compiler, no configuration needed
  • Modern APIs - Web standard APIs (fetch, WebAssembly, etc.)
  • Single executable - No package.json or node_modules
  • Dependency management - Import modules directly from URLs

Installation

Fedora/Linux

curl -fsSL https://deno.land/x/install/install.sh | sh

Add to shell profile (~/.bashrc):

export DENO_INSTALL="/home/$USER/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"

Other platforms

  • macOS: brew install deno
  • Windows: winget install deno

Basic Usage

Running Scripts

deno run script.ts                    # Run TypeScript
deno run --allow-net server.ts        # With network permissions
deno run --allow-all app.ts          # With all permissions

REPL

deno                                  # Interactive shell

Logging in Deno

Deno provides built-in logging capabilities through the standard library.

Basic Logging

import { getLogger } from "https://deno.land/std/log/mod.ts";

const logger = getLogger();
logger.info("Application started");
logger.warn("This is a warning");
logger.error("An error occurred");
INFO Application started

WARN This is a warning

ERROR An error occurred
"An error occurred"

Custom Logger Configuration

import * as log from "https://deno.land/std/log/mod.ts";

await log.setup({
  handlers: {
    console: new log.ConsoleHandler("DEBUG"),
    file: new log.FileHandler("WARN", {
      filename: "./log.txt",
    }),
  },
  loggers: {
    default: {
      level: "DEBUG",
      handlers: ["console", "file"],
    },
  },
});

const logger = log.getLogger();
logger.debug("Debug message");
logger.info("Info message");
DEBUG Debug message

INFO Info message
"Info message"

Log Levels

  • CRITICAL (50)
  • ERROR (40)
  • WARNING (30)
  • INFO (20)
  • DEBUG (10)

Permissions

Deno uses explicit permissions for security:

deno run --allow-read --allow-write file-ops.ts
deno run --allow-net --allow-env server.ts
deno run --allow-all app.ts

Permission flags:

  • --allow-read[=path] - File system read
  • --allow-write[=path] - File system write
  • --allow-net[=host] - Network access
  • --allow-env[=var] - Environment variables
  • --allow-run[=program] - Run subprocesses
  • --allow-ffi - Foreign function interface
  • --allow-hrtime - High resolution time
  • --allow-all - All permissions

Package Management

Import from URLs

import { serve } from "https://deno.land/std@0.208.0/http/server.ts";
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";

Lock files

deno cache --lock=lock.json --lock-write deps.ts

Testing

Built-in test runner:

// math_test.ts
import { assertEquals } from "https://deno.land/std/assert/mod.ts";

Deno.test("addition test", () => {
  assertEquals(2 + 2, 4);
});
addition test ... ok (0ms)



ok | 1 passed | 0 failed (0ms)
deno test                             # Run all tests
deno test math_test.ts               # Run specific test

Configuration

deno.json

{
  "compilerOptions": {
    "allowJs": true,
    "lib": ["deno.window"],
    "strict": true
  },
  "lint": {
    "rules": {
      "tags": ["recommended"],
      "exclude": ["no-unused-vars"]
    }
  },
  "fmt": {
    "useTabs": false,
    "lineWidth": 80,
    "indentWidth": 2,
    "semiColons": true,
    "singleQuote": false
  },
  "tasks": {
    "dev": "deno run --watch main.ts",
    "test": "deno test --allow-all"
  }
}

Development Tools

Built-in tools:

deno fmt                             # Format code
deno lint                            # Lint code  
deno test                            # Run tests
deno doc                             # Generate documentation
deno compile                         # Compile to executable
deno bundle                          # Bundle modules

LSP Integration

Most editors support Deno through the Language Server Protocol: - VS Code: Deno extension - Vim/Neovim: Built-in LSP support - Other editors: Configure LSP client

Web APIs

Deno implements many web standard APIs:

// Fetch API
const response = await fetch("https://api.github.com/users/denoland");
const data = await response.json();
console.log(`Deno has ${data.public_repos} public repositories`);

// Web Streams
const readable = new ReadableStream({
  start(controller) {
    controller.enqueue("Hello ");
    controller.enqueue("World!");
    controller.close();
  }
});

const reader = readable.getReader();
let result = "";
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  result += value;
}
console.log(result);

// WebAssembly - Simple example that adds two numbers
const wasmBytes = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
  0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
  0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20,
  0x00, 0x20, 0x01, 0x6a, 0x0b
]);
const wasmModule = await WebAssembly.instantiate(wasmBytes);
const addFunction = wasmModule.instance.exports.add;
console.log(`WASM add(5, 3) = ${addFunction(5, 3)}`);
Deno has 205 public repositories
Hello World!
WASM add(5, 3) = 8

Footnotes

  1. https://deno.land/↩︎