We are hiring

Deno 1.0 - not a quicky

Deno

Deno, the new Javascript and Typescript runtime from Ryan Dahl (the creator of NodeJS) is releasing 1.0 on May 13. Deno is a general purpose JS/TS programming environment. It ties together the best open-source technologies and offers a small but comprehensive solution.

It takes advantage of the JavaScript features that have been available since NodeJS was released but addresses design flaws. Some call it NodeJS 2 but the author made no such claims. It still uses the V8 engine but rather than being written in C++ its written in Rust. One stand out feature is native Typescript support (it still needs to compile into JavaScript).

Getting started

To download Deno follow the instruction on the homepage. After downloading you can get help using the following commands:

  • deno --help
  • deno [subcommand] --help

In this guide we'll cover all the best features of Deno 1.0 and provide some examples in TypeScript, so lets just jump the fence.

The standard library

The Deno standard library is a collection of common modules that are maintained by Deno and guaranteed to work with Deno. It's based on the standard library provided by Go and features most features for common tasks.

While JavaScript missed a standard library users always needed to search for third-party modules or reinvent the wheel for common problems. Not anymore, you can read more about the standard here.

Here is a break down of the modules with npm equivalents

Deno npm Description
colors chalk, colors, kleur Colors in terminal
datetime - Help with JS Date object
encoding - Support for base32, binary, cvs, toml, yaml
flags minimist Command line arguments
fs - File system
http http-server Serving local files over HTTP
log winston Creating logs
testing chai Unit testing
uuid uuid UUID generation
ws ws Websocket client/server

Security

Deno is secure by default while NodeJS has full access to your file system and network.

To run programs with no permissions :

deno run file.ts

But if your code needs a permission you will be alerted with

error: Uncaught PermissionDenied: access to run a subprocess,
       run again with the --allow-run flag

Using command line options we can allow certain permissions, most commonly :

  • environment access
  • file system read/write
  • network access
  • running a subprocess You can find the fill list with deno run -h

To be even more specific with giving permissions, such as giving write access only to the /home directory.

deno --allow-write=/home

TypeScript

Typescript is a extension of Javascript with added explicit types. Any valid JavaScript file can be converted to TypeScript just by changing the extension to .ts, then add types and its TypeScript.

Want to use TypeScript in Deno ? Well there is nothing you need to do. Deno will compile TypeScript to JavaScript internally.

To use your own tsconfig.js just run

deno run -c my_tsconfig.json [file.ts]

Web standards

Deno uses web standards wherever possible. Web standards are way better to use than frameworks, while a framework may "die" in a few years, web standards can stay for decades.

Deno provides the following web-compatible APIs :

addEventListener atob btoa
clearInterval clearTimeout dispatchEvent
fetch queueMicrotask removeEventListener
setInterval setTimeout AbortSignal
Blob File FormData
Headers ReadableStream Request
Response URL URLSearchParams
console isConsoleInstance location
onload onunload self
window AbortController CustomEvent
DOMException ErrorEvent Event
EventTarget MessageEvent TextDecoder
TextEncoder Worker ImportMeta
Location

EXMAScript modules

Deno zooms straight to modern web standards as seen above, that is true for its module system too. Modules are referenced by using a URL or file path an include a mandatory file extension.

import { Application } from "https://deno.land/x/abc/mod.ts";
import * as args from "https://deno.land/x/args@1.0.7/wrapper.ts";
import {Test} from "./test.ts";

There is just a tiny issue here, for instance if you included the wrapper module in multiple files and you want to update the module. You would have to replace the import in all of them. For that reason Deno uses a file called deps.ts that allows different modules in the application to reference the same file and updating is made a lot easier.

import { parse } from "https://deno.land/std@v0.39.0/flags/mod.ts";
export { red } from "https://deno.land/std@v0.39.0/fmt/colors.ts";

Package management

No more cental repository like npm, Deno uses a decentralized system. If you know how to host a file on the internet you know how to host a package. There is no more centralized package manager, you import ECMAScript modules directly from the web. Then i gets cached for you. No more node_modules directory, dependencies are downloaded and hidden away on your drive.

You can find a list of third party modules here, it's simple for now but i expect it go grow and improve over time. There you can find a module named node, this is a compatibility layer to run the NodeJS API but i suggest you don't use it.

Compiling, bundling and formatting

With Deno you get an compiler API, that way you can create a custom bundle (remember to use the --unstable flag when using unstable APIs). If you have a simple project or are a bit lazy have Deno create the bundle using deno bundle.

For formatting you can use a alternative to Prettier with all the functions included called dprint. use either deno fmt <files> or the Visual Studio Code extension.

Conclusion

While it may not replace Node.js, Deno is already a great programming environment. By using web standards you can build future proof applications with TypeScript already included in the box. There is no need to use a package manager, everything is trying to be standardized. And with JavaScript still growing at a fast pace with a even faster rate of change in the ecosystem one might want something more stable, more high-quality, and not have to deal with a new framework every 3 months. Still, for some, that high rate of change is what keeps them going and thats a positive for good old NodeJS.