Nono.MA

Deno 101

DECEMBER 20, 2023

I was curious about the point of Deno, a JavaScript and TypeScript runtime with similarities to Node.js. Both are built on the V8 JavaScript engine, but Deno is built in Rust; Node is built using C++.

Deno is built for safety and has TypeScript support—there's no need to transpile files to JavaScript.

Two features I love are that you can execute TypeScript directly and that programs can be compiled into standalone binaries with deno compile. Permissions can be specified (say, using --allow-read) on program execution, or be included in the standalone bundle at compile time.

Here's a sample program that watches a directory for file changes, returning create, modify, and remove events.

// watcher.ts
const watcher = Deno.watchFs("/Users/nono/Desktop/files/");

for await (const event of watcher) {
    console.log(`Change detected:`, event);
}

This program can be run with deno run.

deno run watcher.ts

The program will request permissions for /Users/nono/Desktop/files, which you can provide at execution time to avoid the prompt during execution.

deno run --allow-read=/Users/nono/Desktop/files watcher.ts

We can compile the program as a standalone executable bundle.

deno compile --output=my-watcher watcher.ts

The binary can be executed and will ask for the same permissions.

./my-watcher

Permissions can be included at compilation time as well.

deno compile \
--allow-read=/Users/nono/Desktop/nono \
--output=my-watcher watcher.ts

Now, our executable has the required permissions. Not more, not less.

Blog