Deno, what comes after Node.js


Deno is a relatively new runtime for both JavaScript and Typescript. It was released 2 years ago by Ryan Dahl, creator of Node.js, and, because there’s always room for improvement, he wanted to fix the things he considered wrong with Node.js.
There are several differences between the two, but in this article we will focus on three of them: Integrated typescript support, security and package management.

Installation

Installation is very simple and can be performed from the command line.

For macOS and Linux:

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

And for Windows, using Powershell:

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

That’s it, now you can run your first program using Deno like this:

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

As you can see, you can just provide an url of the file without having the source code on your machine.

You can read more on installation information here.

Integrated Typescript support

Typescript and Deno.js are similar since both aim to improve the shortcomings of their predecessors Javascript and Node.js. Having that in mind it makes more sense that Deno offers integrated Typescript support. All you have to do to run a typescript file is:

deno run main.ts

It’s that simple. No more transpilers.

Security

Deno is more focused on security. By default, a program has no permissions (file, network and others) and you need to grant them when you run the code from the command line.

Some of them are:
–allow-net=<allow-net>. You can allow all network calls or specify allowed domains (i.e. –allow-net=github.com,gitlab.com)

–allow-read and –allow-write are similar and like the previous one, you can allow access to any file or specify allowed folders or files (–allow-read=/etc)

A list with all the permissions can be found here.

Package management

In the beginning of the article you’ve seen that you can execute a file using just an url. That applies also to dependencies since we no longer have node_modules and package.json like we did with Node.js.
Using a third party library is not always as easy as it should be, but with some luck you may find your desired module on https://deno.land/x and import it like this:

import format from “https://deno.land/x/date_fns@v2.15.0/format/index.js

Bonus

Let’s get our hands dirty and run some piece of code using Deno. Here’s a simple utility script that looks for duplicates in a list of folders. You can run it like this:

deno run --allow-read --unstable "https://bit.ly/C4N-checkDuplicates" folder1 folder2

You may also delete the duplicates using the –delete / -d flag:

deno run --allow-read --allow-write --unstable 
"https://bit.ly/C4N-checkDuplicates" folder1 folder2 -d

Note that the –allow-write permission was granted so the files can be deleted. Otherwise an error is raised.

Conclusion

As of October 2020, more than two years after the first release, Deno reached version 1.4.4 and it begins to be considered production ready. If you’re not comfortable to use it in your project right now, it would be useful to get to know the new kid in town since you might use it in the near future. Until then, you can start using it instead of bash or python for writing utility scripts.

Previous post

Happy Halloween !

Read More

October 2018 – Code 4 Nord celebrates 4 years of activity

Read More

Comments are closed.