HomeAbout Me

What is Deno.js and how it is different from Node

By Sri Gunnala
Published in Front End
May 05, 2022
3 min read
What is Deno.js and how it is different from Node

Deno is a javascript runtime same as Node, reached its 1.0 release status on May 13, 2020. Deno was developed by Ryan Dahl, the same person who developed Node. Two Javascript run-times by the same person. This raises many questions — What made Ryan develop another Javascript runtime? What is wrong with Node? Is Deno going to replace Node? Let’s find answers to all of these.

Why Deno.js?

Node was designed in 2009 when Javascript was a much different language. Since then, through standards organizations like ECMA International, the Javascript has been improved continuously. For instance, there were no Promises or async/await when Node was developed. Node’s counterpart to promises was the EventEmitter which has a back-pressure issue and later it was mitigated with extra code.

On other hand, out of necessity, Node had to invent concepts that were later taken up by the standards organizations and added to the language differently.

With the continuous evolution of Javascript and new additions like TypeScript, building and managing Node projects needed extra heavy lifting work. Since Node’s first release, Javascript and its ecosystem have changed enough to consider simplifying Node.

In a single line, Node has redesigned to Deno to fix its flaws.

If you are curious and want to understand the design mistakes of Node, check this below video by Ryan Dahl — Regret about Node.js

What is Deno?

According to Deno’s official page, Deno is ‘A secure runtime for JavaScript and TypeScript’. This definition has a very close match to Node except for the terms security and Typescript. Let’s understand this a bit more with a Deno sample and find out how it is different from Node.

How to install Deno?

I am running windows, so I installed it using a power shell. For all install options, look at here.

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

Power Sheel Run
Power Sheel Run

Run the command “deno -V” to confirm successful installation. This will give the current version (1.0 as of writing this) of deno.

A simple Deno Code

Here is the simple typescript code in Deno.

Sample Deno Code
Sample Deno Code

We are importing serve from a web resource, spinning up the server at port 8000 and listening to it. Run the code with the below command.

deno run sample.ts

Deno Run
Deno Run

This will throw an error — Uncaught PermissionDenied error.

Before we fix the error, let’s dig deeper to understand how this code is different from Node.

How Deno is different from Node

  1. Typescript — If you look at the example, it is a typescript file. Yes, Out of the box, deno support Typescript as well without the need for manual compilation. The Typescript compiler is built into deno. You can write all the code in Typescript.
  2. Importing packages — In deno, you don’t need to install any module to import them into code. Instead, you import from the web server. In the example above, we imported the server from the web server URL. When you run the code, it downloads all imports from a web server and catches them locally so it doesn’t need to download them again when you run next time.
  3. Promises/async iterables — In our sample code, the for loop was an infinite array of incoming data and events. Await command doesn’t need to be wrapped up in an async function, deno also supports top-level await. We don’t need any wrapper around await.
  4. Secure runtime — With node when we import third-party packages, we don’t have much visibility and control over them. We are relying on the maintainers of the package and trusting them they don’t write any harmful code under the hood. With deno, we control which permissions we give to our script to execute them. By default, there will be no permissions. This is the reason why our sample got an error Uncaught PermissionDenied error. In our sample we need network access, so we need to pass another flag to give those extra permissions. So, deno is considered to be more highly secured than Node.

Deno or Node?

Deno is an alternative to Node. But it is very new, immature and it will have bugs. It has been in development for 2 years now and there will be a lot of development in the coming year. Deno initial release 1.0 is stable, however, it might not be the right choice for everyone right now. Deno is not compatible with Node packages. All the functionality which is not yet ready for stabilization has been hidden under the ‘- -unstable’ command line flag.

With all these, Node is not going anywhere. It is a huge ecosystem and a lot of company still uses it. Deno will co-exist with Node and might play an important role in the future. But, it is still nice to dive in to start to play around with it and do some experiments.


Tags

#denojs#nodejs
Previous Article
How to log request and response payload in Azure API Management
Sri Gunnala

Sri Gunnala

Learner | Reader | Blogger | Azure Enthusiast

Topics

Front End
Microsoft Azure
Microsoft .NET

Newsletter

Sri Gunnala - Make sure to subscribe to newsletter and be the first to know the news.

Related Posts

Use Font Awesome Angular Component in 5 simple steps
May 05, 2022
1 min

Legal Stuff

Privacy NoticeCookie PolicyTerms Of Use

Social Media