NodeJS Devs: What can Deno Offer Us?

The jury is still out for Deno.
Share on facebook
Share on twitter
Share on linkedin

Get the best of
The Daily Bundle in your inbox every week

NodeJS Deno

Get the best of The Daily Bundle in your inbox every week

Deno recently released its first stable version. As a NodeJS developer, what does it mean for me? Should I switch to Deno (or at least learn it) right now? What can Deno offer me that Node (currently) can’t?

I heard about Deno around 5 months ago when it was on v0.26. Now that it’s trending due to their first stable release, it caught my attention again, and the question just popped into my head: What can Deno offer me? Should I learn more about it and start working more with it? 

I’ve been using Deno on some pet projects since the latest release, reading the documentation, and checking what it currently has to offer. After a while, I’ve compiled a list of Deno’s main selling points you can read below.

 

1. Security out of the box

One of the main premises of Deno. Thanks to its sandbox execution model, we have no network, filesystem nor execution access by default. For instance, we can have this small script to fetch some data from a REST API:

 

REST API Consumption in Deno
const res = await fetch('http://some.rest.api/foo');
const body = new Uint8Array(await res.arrayBuffer());

 

And if we run it using

deno run fetchData.ts

We will get an error like this:

error: Uncaught PermissionDenied: network access to "http://some.rest.api/foo", run again with the --allow-net flag
at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
at Object.sendAsync ($deno$/ops/dispatch_json.ts:98:10)
at async fetch ($deno$/web/fetch.ts:591:27)

Adding the –allow-net flag solves the issue:

deno run --allow-net fetchData.ts

This gives you network access. What if we want to provide granular permission only for certain REST APIs? We can specify one or multiple values in the –allow-net flag:

deno run --allow-net=some.rest.api,another.rest.api fetchData.ts

There’s a similar way to manage access to the filesystem. For that you can use –allow-write and –allow-read alone, to provide full write/read access. You can also be more specific by providing a comma-separated list of directories to allow access to. 

deno run --allow-write=/usr/data --allow-read=/etc/config,/etc/env readAndWriteFiles.ts

 

So far the identified permissions flags that Deno allows are the following ones:

--allow-net //for enabling network access
--allow-write //for enabling write access
--allow-read //for enabling read access
--allow-run //for running commands

I think this is an accurate approach: to deny permissions by default, which forces you to be specific about the accesses your program needs, rather than granting all permissions by default and manually taking care of the restrictions. 

 

2. A curated list of standard libraries with no dependencies

Deno’s development team makes an emphasis on having a curated list of standard modules that have no dependencies with external modules. 

This has the purpose of maintaining a high-quality codebase in which you can create Deno applications and solve common problems without depending on external modules.

For instance, datetime (https://deno.land/std/datetime) offers a very simple yet useful API for generating Date objects based on strings and a specific format. 

Basic usage of standard datetime module
import { parseDate } from 'https://deno.land/std/datetime/mod.ts';
import * as log from "https://deno.land/std/log/mod.ts";
log.info(parseDate("22-05-2020", "dd-mm-yyyy").toString());

 

Personally, I’m more of getting things done by using the minimum (or no) amount of dependencies in a project. Many of these standard library modules are pretty basic at the moment (for instance datetime allows you to parse Strings to date/datetime, but not the opposite). But as it gets more support from the open-source community, they can become more mature and complete.

Also, we have the third-party modules, which are reviewed and then added to the official Deno. So far there are several capable modules, like Oak, which is based on Koa for NodeJS, already including routing and body parsing in the framework.

Basic web server specifying a route in GET using Oak
import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const app = new Application();const router = new Router();

router.get("/gimmefoo", (context) => {
context.response.body = {foo: "bar"};
 })
app.use(router.routes())
app.use(router.allowedMethods());
await app.listen({ port: 8079 });

You might have noticed that unlike npm require, you use ES imports by default, but referencing the library or third party module by the URL. You are able to use a specific version of a module by adding it to the URL, otherwise, it defaults to the latest version.

ES6 import syntax

NodeJS Deno Developers

It certainly is a different approach than npm’s. It’s true that it could look cumbersome to have a bunch of URLs in your source code instead of module names, but there are always workarounds to deal with it. Time will tell if a package management system will be implemented in the future.

 

3. Typescript by default, built-in Rust

Instead of being built in C++ like NodeJS, Deno is based on Rust and some Rust libraries like Tokio (being the equivalent of livuv for Node).

Deno is built on a solid foundation that will allow it to grow healthy and to become a performant runtime environment. It even resembles some features that can be found on the Rust ecosystem, like the fmt command tool, which allows you to format source code (similar to cargo format).

 

Using Deno fmt command to format source code

Deno Developer

 

Another good thing is having typescript by default. JavaScript is a very beautiful language in some aspects, but it’s true that sometimes a bit more strictness is needed, especially on the backend side. No need for a tsconfig file in your project, just start coding and it will work.

Right now there’s a lack of development environments that support working on Deno projects, but that won’t be an issue soon, as some of the most popular tools like VSC and Webstorm are already working on the creation of plugins to support it.

 

Waiting for major IDEs to provide support through plugins

Deno Code for NodeJS Developers

 

Bottom Line

Are these selling points enough to make you move to Deno? Although there are more, certainly not. At least not moving entirely to work on Deno apps per se. However, it sure is a good time to start learning about it and keep track of it to see if it grows healthy as more companies and startups start adopting it.

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on email
Scroll to Top