Deno vs Node – Deno tutorial and introduction

4 min

read

As far as Javascript runtime environments go, there is Node and not much more. But the creator of Deno, a former Node.js founder, decided to change that. Now, that Deno is released, it’s time to find out if it can prove a worthy competition for Node.js.

The Deno project started back in 2018 when Node.js creator Ryan Dahl announced the development of Deno in a talk called “10 Things I Regret About Node.js”. Yes, it says it all. So what’s the deal and why we need an alternative to Node anyway?

What are we going to cover?

In this article, you’re going to learn:

  • what is the Deno software and why it can be considered an alternative to Node,
  • the main differences between Deno and Node in terms of approach to various web development issues and performance,
  • what is it like to write a simple Deno app,
  • is Deno mature enough to be used in production already?,
  • is it worth the effort to learn for an experienced Node developer?

Let’s get right to it!

What is Deno?

Deno is a safe and stable runtime environment for JavaScript and TypeScript baked on the V8 engine (TypeScript support is a big selling point for Deno). It’s written in Rust. The creators of Deno had the intention of taking full advantage of all the latest features of the JavaScript language. For example, its API supports the Promise object and uses the ES module as a default module system. They also designed to be self-sufficient – no external dependencies required. Safety is a major design priority in Deno. All-access operations to the disc or online must be explicitly issued in the script.

Deno vs Node.js

Deno creators have every intention to provide a strong alternative to Node.js. Hence, it’s no surprise that even though they have a lot in common, there are a couple of major differences between these two. Let’s take a look at a simple side-by-side comparison:

DENO 

NODE 

Uses V8Uses V8
Written in RUST and TypeScript Written in C++ and JavaScript 
Run in the sandbox with limited access – explicit command issue in the script required.The question of access is limited to the specific access rights of a user that runs the script.
Decentralized modules – loaded from an URL.NPM 
ES Module CommonJs 
The API and the standard library takes full advantage of ES and Promise.The API and standard library based on callbacks.

 

Let’s practice!

As with any piece of technology, practice beats the theory. But before we get to the best part, let’s first install the Deno platform and quickly go over the app structure.

Installation

All you get is a single executable file without any extra dependencies. You can do it via an OS package manager. For example, for Homebrew, the command will be brew install deno. You can also get the binary files straight from the official Deno repository.

App structure

One thing that really stands out from the beginning is the fact that you can write your app directly in TypeScript. You don’t need to compile to JavaScript yourself. In addition, a method to compile  TypeScript using Rust is also currently in the planning phase.

Deno’s creators made the bold decision to get rid of a centralized package manager, such as npm or NuGet. Instead, the code is loaded directly from the URL. Once you run a script, the packages are downloaded from a specified location and then cached.

import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts"; 

What’s also interesting is that you can use various versions of a given library in one file. All you need to do is import it and add aliases.

import { Application as AppOld } from "https://deno.land/x/[email protected]/mod.ts"; 

Simple module-based application & unit testing

Let’s write a simple script that takes numbers as parameters. Then, we’re going to use a module to add all integers. The result is going to be output to the console.

We’re going to use built-in tools to write a simple test for the module. To simplify things, we can convert all parameters to integers.

Let’s make three files.

  • index.ts – the main script of the application,
  • mathService.ts – a class that has a public method that multiplies a number by two,
  • mathService.test.ts – a test for the multiply class.

Deno has a built-in test runner and ready-made asserts. You can view them here. At this moment, there is no information on creating stubs (mocks).

We can launch the tests directly from the console with the deno test command.

$ deno test 

running 1 tests 

test MathService ... ok (3ms) 

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (4ms)

All there’s left to do is to write the main script. It’s worth it to notice that Deno gives us direct access to the Deno namespace in all the scripts.

To run the script, we go to the console and run the command: 

$ deno run index.ts 2 7 6

Simple API

Simple shell-based scripts are hardly the extent of Deno’s ability. Ever since Deno was released, developers have been adding more and more 3rd party libraries. Even though (much like it was the case with Node at the beginning), some of them are quite unpolished.

Let’s now use the capabilities of the denotrain library to create a simple API. This library is clearly inspired by Node’s Express.js so the code may seem familiar. To run a basic API, we only need one index.ts file and 8 lines of code.

It’s a good moment to mention that Deno supports Top-Level Await (last line of the code above). It’s a different story if this is the right solution, but it is sufficient for this simple example.

We can now run the script with the command below (granting access to the network in the process):

$ deno run --allow-net index.ts 

Serving on http://0.0.0.0:3000/

Deno – summary

As you learn more and more about Deno, the one question you probably have in mind is whether Deno can replace or at least be a strong alternative to Node.js. Node’s doing great and I can’t think of a good reason to give up on it. At this point in time, the best way of thinking about Deno is as an interesting alternative rather than a one-to-one replacement.

Even though the 1.0 version got a proper release, I’d still be wary of writing production code in Deno. I just don’t think that it’s mature enough. Another challenge is to properly protect our app from external modules. We are bound to come across challenges such as temporary unavailability of a package. On the other hand, a very interesting aspect of Deno is the ability to write shell scripts in TypeScript.

It’s definitely worth it to keep exploring Deno because in the future it might just become an important player in the currently (too?) heavily monopolized field of JavaScript runtime environments. Especially that the barrier to entry for Node.js developers is practically non-existent. So do give it a try and if you find it interesting, regardless of what it means for you, share your own experiences. Let’s make the landscape of JS runtime environments more exciting!

What do you want to achieve?





You can upload a file (optional)

Upload file

File should be .pdf, .doc, .docx, .rtf, .jpg, .jpeg, .png format, max size 5 MB

Uploaded
0 % of

or contact us directly at [email protected]

This site is protected by reCAPTCHA and the Google
Privacy Policy and Terms of Service apply.

Thanks

Thank you!

Your message has been sent. We’ll get back to you in 24 hours.

Back to page
24h

We’ll get back to you in 24 hours

to get to know each other and address your needs as quick as possible.

Strategy

We'll work together on possible scenarios

for the software development strategy in sync with your goals.

Strategy

We’ll turn the strategy into an actionable plan

and provide you with experienced development teams to execute it.

Our work was featured in:

Tech Crunch
Forbes
Business Insider

Aplikujesz do

The Software House

CopiedTekst skopiowany!

Nie zapomnij dodać klauzuli:

Kopiuj do schowka

Jakie będą kolejne kroki?

Phone

Rozmowa telefoniczna

Krótka rozmowa o twoim doświadczeniu,
umiejętnościach i oczekiwaniach.

Test task

Zadanie testowe

Praktyczne zadanie sprawdzające dokładnie
poziom twoich umiejętności.

Meeting

Spotkanie w biurze

Rozmowa w biurze The Software House,
pozwalająca nam się lepiej poznać.

Response 200

Response 200

Ostateczna odpowiedź i propozycja
finansowa (w ciągu kilku dni od spotkania).

spinner