Top 5 Reasons Javascript Developers Prefer Deno Over Node

A Side-By-Side Comparison

Dan Halperin
Level Up Coding

--

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…”. Don’t worry, I had the same reaction. After understanding the advantages, I too saw 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.

1: Modern Javascript — ES Modules

If you’re a React developer like me, you’ve noticed that the syntax for importing packages is different when working with NodeJS. This is because Node was made back in 2009, there’s been a lot of updates and improvements to Javascript since then.

In React (and Deno), we use the modern import package from 'package' syntax while in Node we use the const package = require("package") syntax.

The ES Modules import is superior for two reasons:
1: With import, you can selectively load only the pieces you need from a package, which saves memory.
2: Loading is synchronous with require while import loads modules asynchronously, which improves performance.

If you noticed in the image above, we are importing the moment package from a URL, which leads us to the next advantage of Deno.

2: Decentralized Packages

With Deno, you are no longer dependant on NPM. That’s right, no more package.json. Every package is loaded in from a URL.

In NodeJS, to use a package you would have to first install it from NPM:

npm i moment

Wait for it to install, then include it in your app:

const moment = require("moment")

Also, any time someone wants to run your NodeJS repo locally they would have to install all the dependencies from NPM.

In Deno, the package is imported from a URL, so if you want to use moment you would just import https://deno.land/x/moment/moment.ts.

Another huge advantage when it comes to packages in Deno is that every package is cached on the hard drive after installation. This means installation of a package happens only once. If you want to import the dependency again, anywhere, it will not have to be downloaded.

3: TypeScript works natively, no configuration needed

Getting TypeScript to work with NodeJS is a multi step process. You’d have to install typescript, update the package.json, tsconfig.json, and make sure your modules have @types supported.

In Deno, all you have to do is save your file as .ts instead of .js, the TypeScript compiler is already baked right in.

4: Top level await —Use await outside of an async function

In Node, the await keyword is only accessible in an asynchronous function.

With Deno, you can await anything anywhere, without wrapping it in an async function.
Almost all Javascript apps include many async functions. This upgrade makes the code much more clean and simple.

5: Access to the Browser API (Window, Fetch)

To make HTTP requests in javascript, we can use the Fetch API.
In NodeJS, we do not have access to the Browser API so we can not natively call a fetch function. We must first install the package:

npm i node-fetch

Then import the package:

const fetch = require("node-fetch")

And only then we can make a fetch call.

Deno natively has access to the window object, which means you can just go ahead and call fetch("https://something.com") as well as any else in the Browser API without installing any libraries.

When combined with the top level await advantage, you can now see how much simpler Deno code is than Node code:

A more 2020 way of writing Javascript code

It doesn’t end there

Deno has many other advantages, like being more secure by default, you can execute Wasm binaries, it has many built in libraries, and the list goes on.

All points mentioned in this article are interconnected, coming together to form a more modern 2020 back end javascript runtime. As a React dev I resonate with Deno. I can now use the import syntax, write await wherever I want, use TypeScript without configuring anything, and even call fetch without having to install a package. It’s just easy.

Will Deno eventually replace Node? Maybe. It will likely take several years. The NodeJS ecosystem is huge, it will take time for Deno to catch up. But Javascript developers have recently been favoring Deno for their new projects, so if you’re looking to start a new project soon it’s definitely worth looking into. For more information on how to get started, visit deno.land

--

--