Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

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:

This is image title

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.
export class MathService { 
  sum(numbers: number[]) { 
    return numbers.reduce((value, number) => Number.isInteger(number) ? value + number : value, 0) 
  }
} 

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).

import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; 
import { MathService } from './mathService.ts'; 

Deno.test('MathService', () => { 
  // given 
  const sut = new MathService(); 

  // when 
  const result = sut.sum([1, 3, 5]); 

  // then 
  assertEquals(result, 9); 
}) 

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.

import { MathService } from './mathService.ts'; 

const mathService = new MathService(); 
const numbers: number[] = []; 

Deno.args.forEach(number => numbers.push(parseInt(number, 10))); 

console.log(`Result: ${mathService.sum(numbers)}`); 

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 i_ndex.ts_ file and 8 lines of code.

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

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

routes.get("/status",() => { 
  return 'ok'; 
}); 

app.use("/api",routes); 

await app.run(); 

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!

deno javascript node web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top 5 Reasons Javascript Developers Prefer Deno Over Node

Why Deno is exactly what a back-end Javascript developer needs in 2020. Let’s take a look at the top 5 reasons javascript developers are having a much smoother and more modern experience using Deno vs. Node. The maker of NodeJS, Ryan Dahl, has released a new runtime that aims to solve many of the shortcomings of Node. Your initial reaction might be “Oh great, another Javascript framework? Just what I needed…”.

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Deno vs. Node: A Good Crisp Difference Between Node and Deno

Deno vs. Node: A Good Crisp Difference Between Node and Deno - Here is a basic tutorial on a crisp difference between Nodejs and Deno. Before starting with Deno it good to understand what problem deno solves.