Introduction to Deno

Introduction to Deno

Captions converted to content by a course student.

##Deno 1.0: Let's Explore and Play!

Deno

Deno is an amazing, secure runtime for JavaScript and TypeScript. Imagine writing TypeScript without any config files and bundling it all together into a single ES Module, where TypeScript support and a bundler are present in the core. That's the experience you get with Deno!

It's a modern and secure runtime that uses V8 and is built in Rust, unlike Node.js, which is written in C++ and JavaScript.

Fun fact: Deno is an anagram of Node. If you sort() node, it becomes deno.

"node".split("").sort().join(""); // Output: deno

Also, Deno is created by the creator of Node, Ryan Dahl. Yet, Deno is not Node and Node is not Deno. OK, I'll stop with the dad jokes now.

The core comes with many required features for writing modern JavaScript & TypeScript, and WebAssembly code, such as:

  • 📦 Bundler
  • 🐛 Debugger
  • 🤖 Test runner
  • 🧶 Code formatter
  • 📖 Docs generator
  • 🗃 Dependency inspector
  • 🧵 WebAssembly support

More interesting features include…

  • Secure by default: No file, network, or environment access unless explicitly enabled.
  • Single Executable: Ships only a single executable file.

Install Deno

With Shell:

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

With Homebrew:

brew install deno

Now check if deno was installed by running the deno --version command in your terminal.

Deno Completions

You can generate a completions script for your shell using the deno completions command. The command outputs to stdout, so you should redirect it to an appropriate file.

The supported shells are:

  • zsh
  • bash
  • fish
  • elvish
  • Powershell

For example, for bash:

deno completions bash > /usr/local/etc/bash_completion.d/deno.bash
source /usr/local/etc/bash_completion.d/deno.bash

See Deno install documentation for more installation options.

Deno Documentation

Deno Code Examples

Example #1: Deno Hello World

Run the code:

deno run https://deno.land/std/examples/welcome.ts

The code for this file is a simple console log.

console.log("Hello Deno 🦕");

The output should look like this:

Example #2: Deno cat Example

Let's implement a Unix cat program that outputs the contents of a file. Create a file called cat.ts with the following content in it.

// An implementation of the Unix "cat" program.
for (let i = 0; i < Deno.args.length; i++) {
  let filename = Deno.args[i];
  let file = await Deno.open(filename);
  await Deno.copy(file, Deno.stdout);
  file.close();
}
  • Deno.args are the arguments received by this file.
  • Deno.open() opens a file.
  • Deno.copy(file, Deno.stdout) copies the file from the source to the destination, i.e., the standard output.

Let's run this file with the following command and read the welcome.ts that we wrote in the first example.

deno run cat.ts welcome.ts

Uh-oh. Remember, Deno is secure by default. That means we need to allow file read access to run this program. Let's do that by reforming the command and adding the --allow-read flag right after the run keyword.

deno run --allow-read cat.ts welcome.ts

Example #3: Deno Server Example

Let's create a Deno server. The code for this looks pretty simple. Create a file called server.ts with the following code.

import { serve } from "https://deno.land/std/http/server.ts";

const server = serve({ port: 8000 });

console.log("Running at: http://localhost:8000/");

for await (const req of server) {
  const body = "Hello Deno!";
  req.respond({ body });
}

You might be thinking, what the heck is that await doing there? Well, Deno implements a top-level await, a new feature for ES Modules in the JavaScript V8 Engine. Basically, it doesn't need an enclosing async function to run await in the top-level scope.

Now let's run the following command:

deno run server.ts

Nops. That won't work. We haven't allowed it permission to access the net. Let's allow access with the --allow-net flag.

deno run --allow-net server.ts

…and that's about it.

Peace! ✌️