Meet Deno - The Humble Giant

Meet Deno - The Humble Giant

Written by Gopakumar Gopalan,
on Wednesday, May 27, 2020

If you are using Node.js, you probably know the brain behind the revolutionary javascript runner. Yes, Ryan Dahl comes back with a competing product. Meet Deno - The humble giant ("Deeno" as it pronounced by Ryan Dahl); a single file runtime for javascript and typescript. The name Deno is a anagram of Node, as tweeted on the official twitter account.

Deno comes with Typescript support out of the box along with Javascript. Ryan Dahl created Deno to overcome the issues and shortfalls that NodeJs have. Let us have a closer look into Deno and its features.

Secured by Default

Deno secures the execution by default not allow access to disk, network, subprocesses, or environmental variables. For example if you need to run a script with network access like a server then you need to add --allow-net flag on your run command.

deno run --allow-net server.ts

Other flags are --allow-read to read a file --allow-write to write to a file --allow-run to run a subprocess, --allow-env to access environment variables. You can provide all access with flag --allow-all or -A, which is not recommended.

TypeScript Support

Deno supports TypeScript out of the box. As a typed superset of JavaScript, your code will be more robust and less error prone. You can use all ES6 features with TypeScript. In NodeJs your TypeScript code is transcoded down to JavaScript before execution. With Deno you can skip transcoding and run the code directly.

Standard Library

Deno comes with a set of Standard Modules under Standard Library. These modules do not have external dependencies and they are reviewed by the Deno core team. The intention is to have a standard set of high quality code that all Deno projects can use fearlessly. All these modules are tagged as of Deno releases and you can access a standard module of version 1.0.0 with your import URL starting https://deno.land/[email protected]/. You will get the latest version if you simply import without a version tag https://deno.land/std/

Let us import the testing module for our application. So I have Deno version 1.0.0 and the latest is version 1.0.1 (assumption). It is safe to use the version 1.0.0 module for the application.

import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("example", function (): void {
  assertEquals("world", "world");
  assertEquals({ hello: "world" }, { hello: "world" });
});

Built-in Formatter

Deno ships with a built-in code formatter which formats your JavaScript and TypeScript files.

# format all JS/TS files in the current directory and subdirectories
deno fmt
# format specific files
deno fmt myfile1.ts myfile2.ts
# check if all the JS/TS files in the current directory and subdirectories are formatted
deno fmt --check
# format stdin and write to stdout
cat file.ts | deno fmt -

You can skip a code block from formatting with a // deno-fmt-ignore comment on top of the code block.

// deno-fmt-ignore
export const identity = [
    1, 0, 0,
    0, 1, 0,
    0, 0, 1,
];

Or your can put // deno-fmt-ignore-file at the first line of a file to skip the entire file from code formatting.

Modules Import

In Deno land there is no scary things like node_modules directory which some times have an infinite trail of dependencies or bloated packge.json. Deno has no centralized repository for modules like NodeJS have NPM. You can have your module hosted anywhere. You can access it by importing it from the URL.

// Import Module
import {MyAwesomeFeature} from "https://mywebsite/path/to/myawesomemodule.ts";

// Assignment
const awesomeness = new MyAwesomeFeature();

// Use Function
awesomeness.spreadLove();

All your imported modules will be cached on first run or as you import with plug-ins in your editor. 

As mentioned before, Deno have Standard Library which have my Standard Modules maintained by Deno Authors. It also have a Third Party Modules section where you can also contribute. All listed third-party modules are imported from https://deno.land/x/. You can find modules like oak (https://deno.land/x/oak/) which is a Deno equivalent to Express.

Top-Level Await Support

Top-level await support allows you to execute an awaited function without wrapping it in an async function. 

for await (const req of s) {
  req.respond({ body: 'Hello World\n' })
}

What's cooking ?

Deno is in its early stage. There are many features and tools are under production. Tools like Documentation Generator are under production at the time of this article. WebAssembly is not fully supported currently, you can expect that too in the near future.

Will Deno replace Node?

No, Node is an established eco-system and there are millions of applications which runs on Node. Node is improving, as I am writing this article I heard, now Node (v14.3.0) also support top level awaits. Node is here to stay and Deno will be a good competitor.

Should I learn Deno?

As a NodeJS developer, your learning curve for Deno is near to nothing. Deno is new in the market and there will not be any job opportunity in this beginning  stage. You will gain experience if you adopt it at this stage and grow along with it. In my opinion, Deno is worth learning, though you need to wait for a long time to use it in production. In a few years, Deno will be as popular as Node.

If you are using Node.js, you probably know the brain behind the revolutionary javascript runner. Yes, Ryan Dahl comes back with a competing product. Meet Deno - The humble giant ("Deeno" as it pronounced by Ryan Dahl); a single file runtime for javascript and typescript. The name Deno is a anagram of Node, as tweeted on the official twitter account.

Deno comes with Typescript support out of the box along with Javascript. Ryan Dahl created Deno to overcome the issues and shortfalls that NodeJs have. Let us have a closer look into Deno and its features.

Secured by Default

Deno secures the execution by default not allow access to disk, network, subprocesses, or environmental variables. For example if you need to run a script with network access like a server then you need to add --allow-net flag on your run command.

deno run --allow-net server.ts

Other flags are --allow-read to read a file --allow-write to write to a file --allow-run to run a subprocess, --allow-env to access environment variables. You can provide all access with flag --allow-all or -A, which is not recommended.

TypeScript Support

Deno supports TypeScript out of the box. As a typed superset of JavaScript, your code will be more robust and less error prone. You can use all ES6 features with TypeScript. In NodeJs your TypeScript code is transcoded down to JavaScript before execution. With Deno you can skip transcoding and run the code directly.

Standard Library

Deno comes with a set of Standard Modules under Standard Library. These modules do not have external dependencies and they are reviewed by the Deno core team. The intention is to have a standard set of high quality code that all Deno projects can use fearlessly. All these modules are tagged as of Deno releases and you can access a standard module of version 1.0.0 with your import URL starting https://deno.land/[email protected]/. You will get the latest version if you simply import without a version tag https://deno.land/std/

Let us import the testing module for our application. So I have Deno version 1.0.0 and the latest is version 1.0.1 (assumption). It is safe to use the version 1.0.0 module for the application.

import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("example", function (): void {
  assertEquals("world", "world");
  assertEquals({ hello: "world" }, { hello: "world" });
});

Built-in Formatter

Deno ships with a built-in code formatter which formats your JavaScript and TypeScript files.

# format all JS/TS files in the current directory and subdirectories
deno fmt
# format specific files
deno fmt myfile1.ts myfile2.ts
# check if all the JS/TS files in the current directory and subdirectories are formatted
deno fmt --check
# format stdin and write to stdout
cat file.ts | deno fmt -

You can skip a code block from formatting with a // deno-fmt-ignore comment on top of the code block.

// deno-fmt-ignore
export const identity = [
    1, 0, 0,
    0, 1, 0,
    0, 0, 1,
];

Or your can put // deno-fmt-ignore-file at the first line of a file to skip the entire file from code formatting.

Modules Import

In Deno land there is no scary things like node_modules directory which some times have an infinite trail of dependencies or bloated packge.json. Deno has no centralized repository for modules like NodeJS have NPM. You can have your module hosted anywhere. You can access it by importing it from the URL.

// Import Module
import {MyAwesomeFeature} from "https://mywebsite/path/to/myawesomemodule.ts";

// Assignment
const awesomeness = new MyAwesomeFeature();

// Use Function
awesomeness.spreadLove();

All your imported modules will be cached on first run or as you import with plug-ins in your editor. 

As mentioned before, Deno have Standard Library which have my Standard Modules maintained by Deno Authors. It also have a Third Party Modules section where you can also contribute. All listed third-party modules are imported from https://deno.land/x/. You can find modules like oak (https://deno.land/x/oak/) which is a Deno equivalent to Express.

Top-Level Await Support

Top-level await support allows you to execute an awaited function without wrapping it in an async function. 

for await (const req of s) {
  req.respond({ body: 'Hello World\n' })
}

What's cooking ?

Deno is in its early stage. There are many features and tools are under production. Tools like Documentation Generator are under production at the time of this article. WebAssembly is not fully supported currently, you can expect that too in the near future.

Will Deno replace Node?

No, Node is an established eco-system and there are millions of applications which runs on Node. Node is improving, as I am writing this article I heard, now Node (v14.3.0) also support top level awaits. Node is here to stay and Deno will be a good competitor.

Should I learn Deno?

As a NodeJS developer, your learning curve for Deno is near to nothing. Deno is new in the market and there will not be any job opportunity in this beginning  stage. You will gain experience if you adopt it at this stage and grow along with it. In my opinion, Deno is worth learning, though you need to wait for a long time to use it in production. In a few years, Deno will be as popular as Node.

Related Tags :

> @GKWrites > Meet Deno - The Humble Giant