Ricardo Huamani

What is Deno JS? Introduction to Deno

June 14, 2020

From its home page, it’s a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

But before we dive in on some features of Deno, I’d like to tell you a little history.

History

Ryan Dahl, the creator of NodeJS, created Node focused on programming HTTP servers and lead its initial development.

He left in 2012 and at that moment Node already supported protocols like HTTP and SSL. It worked well on Windows, Linux, and Mac, had a small but stable API, and a growing ecosystem around NPM.

But after 6 years in June 2018, he talked about his regrets with Node, the lack it has, and presented Deno to the community (Deno was born in 2018).

Finally, he released Deno v1 on 13th May 2020.

What’s new on Deno?

Firstly, not only JavaScript but Deno also supports TypeScript natively. So we can either write our code in TypeScript or JavaScript and import it to another file. We can also use both languages in the same project but in different files.

Deno uses ES modules and doesn’t support require(), that is to import or export modules we need to use import and export respectively. We can import modules via URLs from any location on the web like GitHub, a personal web server, or a CDN that uses ES modules.

drihu.com deno

Deno uses V8 which is very secure like Node does. But in Node’s case, it has access by default to our network and our file system. That means our linter would have access to our network and files too. With Deno, that’s not the case. If you want to access your files or network with Deno, you need to enable explicitly at the moment you run your application.

Deno doesn’t use NPM nor package.json. To use libraries in Node, you’re obliged to generate a package.json file and install your modules with NPM. This generates a complex and massive node_modules folder inside your project folder.

Without a package file, we won’t have a really big node_modules folder. We also get freed from all those metadata like license, repository, and description. In Deno, to import a module we only need to specify the location of our file with its extension, e.g. ./script.js.

Another innecessary thing in Node is the index.js file inside a module. When you import a module, Node starts searching for an index file, which complicates the module loading system.

Node uses GYP as its meta build system (a meta build system generates another build system) for compiling native addons written in C and linking them to Node. It was used firstly on Chrome but dropped by Google now, who’s using GN which is 20 times faster and simpler than GYP. So yes, Deno uses GN.

Said all that, I’d like to give you a small introduction on how to install and use Deno.

How to install Deno

If we’re using Linux or Mac, we only need to run the next shell command.

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

On Windows.

$ iwr https://deno.land/x/install/install.ps1 -useb | iex

Or if we’re using Cargo (Rust’s package manager).

$ cargo install deno

How to use Deno

To run a Javascript or TypeScript file with Deno, we just have to run it like the following.

$ deno run script.js

We can also run it from a URL. You can test it in your terminal.

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

If we want to run a server or have access to the network we need a flag to enable that feature. We can specify the origin too.

$ deno run --allow-net script.js
$ deno run --allow-net=example.com script.js

If we want to read or write files inside our system we need the respective flags.

$ deno run --allow-read script.js
$ deno run --allow-write script.js

We can search for and import third party modules from the Deno’s official page deno.land/x with the next format: https://deno.land/x/MODULE_NAME@BRANCH/SCRIPT.ts.

import { Response } from 'https://deno.land/std@0.53.0/http/server.ts';

If we don’t specify the branch, it will use the master branch by default.

One last comment, at this moment Deno hasn’t been tested in production environments yet. So be careful and play with it.

And that’s all. Thanks for reading.


Written by Ricardo Huamani Parian. Full-stack web developer, autodidact, and technical writer. I enjoy coding and sharing about technology. You can follow me on Twitter