Back to all blogposts

Deno tutorial – practical overview of Node.js' rival

Marcin Wosz

Marcin Wosz

Node.js Developer

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. Learn more about how it works and how it differs from Node.js in this practical Deno tutorial.

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” – quite a self-explanatory title, isn’t it?

Yes, it says it all. So what’s the deal and why do we need an alternative to Node anyway?

What are we going to cover in this Deno tutorial?

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 – we’re going to create a simple project just for this occasion,
  • 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 it 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.

In 2021, Deno was backed by some major venture capital initiatives, including Shasta Ventures and Mozilla Corporation. That same year, the creators released Deno Deploy, a distributed system and asynchronous web server that runs JavaScript, TypeScript, and WebAssembly. As of May 2022, Deno’s version 1.22 is the latest stable release.

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.

How important are Node.js and Deno for The Software House's technology stack?

You can learn about this and more using our Technology Radar. Check it out!

Deno in practice

We can compare Deno and Node as much as we want, but at the end of the day, Deno is a JavaScript runtime in its own rights. Let’s get right to it and see how it works!

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.

Deno 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/

Want to practice Deno some more? Make Deno REST API with us!

Deno tutorial – 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!

Both Deno and Node.js go hand in hand with microservices.

If you want to learn more about microservices trends, check out our State of Microservices 2020 report.

For you?
Acceleration Sprints™

You CAN have time for refinement. Run a 1-2 week sprint to improve product metrics soon

How?

The Software House is promoting EU projects and driving innovation with the support of EU funds

What would you like to do?

    Your personal data will be processed in order to handle your question, and their administrator will be The Software House sp. z o.o. with its registered office in Gliwice. Other information regarding the processing of personal data, including information on your rights, can be found in our Privacy Policy.

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

    We regard the TSH team as co-founders in our business. The entire team from The Software House has invested an incredible amount of time to truly understand our business, our users and their needs.

    Eyass Shakrah

    Co-Founder of Pet Media Group

    Thanks

    Thank you for your inquiry!

    We'll be back to you shortly to discuss your needs in more detail.