Deno vs Node: The differences you need to know

Since its announcement, Deno has generated quite a lot of interest within the JavaScript community. As a JavaScript runtime, you might expect there to know a lot of similarities between the two projects. However, there are also important differences between Deno vs Node, and we will show you in this article.

Basic information

What is Deno and its main features?

Deno is a secure TypeScript runtime built on V8, the Google runtime engine for JavaScript.

deno

They built it with:

  • Rust
  • Tokio (the event loop written in Rust)
  • TypeScript (Deno supports both JavaScript and TypeScript out of the box)
  • V8 (Google’s JavaScript runtime used in Chrome and Node, among others)

About Node js

Node.js is an open-source server side runtime environment built on Chrome’s V8 JavaScript engine. It provides an event driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side application using JavaScript.

Deno vs Node comparison

They use Node.js to build different types of applications such as command line application, web application, real-time chat application, REST API server etc. However, it is mainly used to build network programs like web servers, similar to PHP, Java, or ASP.NET.

Learn more about Node Js: Node js: A comprehensive guide to use with examples

The differences between Deno vs Node

APIs

Deno and Node both expose their own APIs to developers, allowing us to write programs that can actually do useful things like read and write files, and send and receive network requests.

Node.js

When Node was first released, there was no built-in support for Promises. As a result, most of the APIs for asynchronous operations can take an error-first callback:

const fs = require('fs');
fs.readFile('readme.txt', (err, data) => {
  if (err) {
    // Handle the error
  }
  // Otherwise handle the data
});

Even though Node developers now have access to Promises and the async/await syntax, the APIs still expect callbacks in order to maintain backwards compatibility.

Deno

They has designed Deno’s API to take advantage of modern JavaScript features. All the asynchronous methods return Promises. Deno also supports top level await. That means you can use await in your main script without having to wrap it in an async function.

try {
  const data = await Deno.readFile('readme.txt');
  // Handle the data
} catch (e) {
  // Handle the error
}

The development team also made the decision to use web standards where possible. That means they’ve implemented browser APIs where it’s practical to do so. Deno provides a global window object. And APIs such as addEventListener and fetch. Having access to fetch is particularly nice, as with Node you’d have to polyfill this or use a third-party library.

The difference of Language Support between Deno vs Node

Both projects are JavaScript runtimes, allowing JavaScript code to be executed on a computer outside of a web browser. Let’s look at how they stack up in terms of language support.

Node.js

The current LTS release of Node (v12.18.1 as of writing) supports modern JavaScript syntax and features. It also supports about 75% of the ES2020 spec. ECMAScript modules are also supported, but are currently only classed as experimental: you need to use the .mjs file extension, or add the property "type": "module" to your project’s package.json file.

In order to run TypeScript (or any other language) on Node, they need to comply the code to JavaScript that the V8 engine can execute. There are several different ways to do this, with different pros and cons. So getting up and running means having to choose one of these and follow the necessary setup process.

Deno

I was unable to find any mention of the JavaScript spec supported by Deno, but as it also uses V8 under the hood I would assume a similar level of support as in Node. My own tests show that Deno supports ES2020 features like Promise.allSettled() and the globalThis keyword. ECMAScript modules are the default, with CommonJS modules not supported unless you use the Node compatibility library (more about this later).

TypeScript is supported as a first-class language in Deno. It means that it works out-of-the-box: no installing additional tools to transpile to JavaScript first. Of course, the V8 engine doesn’t support TypeScript natively. So Deno is still transpiling the code under the hood, but this is all seamless and transparent to you as a developer.

I also couldn’t find mention of which version of TypeScript Deno v1.0.1 uses, but it supports optional chaining and nullish coalescing (but not private class fields) which would peg it as TS 3.7.

Compare Deno vs Node in terms of Package Management

Package management is one area where Deno represents a radical departure from Node’s way of doing things. As it’s still early days for Deno, we can see if its approach will prove to be advantageous.

Node.js

As you might be aware, Node comes with its own package manager called npm used to install and manage third-party packages. They mainly use npm with the online npm registry, where most of the available listed third-party packages.

When you use npm to install a package into your project, a package.json file is used to specify the package name and acceptable version range. The package itself (plus any packages it depends on) are then downloaded into a node_modules folder inside your project.

Deno

Deno does away with the need for a package manager altogether. Instead, linking packages to directly via a URL:

import { Response } from "https://deno.land/std@0.53.0/http/server.ts";

On the first run of your code, Deno fetches and compiles all the dependencies. They are then cached on the file system, separately from your project. So subsequent runs are much faster.

Similar to npm’s package-lock.json file, Deno allows you to specify a lock file that will be used to ensure that only dependencies that match the exact version you originally imported will be used

Third-party Packages

A language can thrive or die on the vibrancy of its ecosystem, as productivity relies on not having to reinvent the wheel! Here, it seems that Node currently has the edge.

Node.js

Node has a large and varied ecosystem of libraries and packages available. In the 11 years since its release, over a million packages have been registered on the npm registry. Of course, the quality can vary a lot. Moreover, many are no longer actively maintained. But it’s still a big plus for Node developers.

Deno

As we saw in the previous section, Deno is actively trying to avoid the need for a package manager or registry, by allowing scripts to import modules directly from any public URL. Of course, it’s hard to import packages if you don’t know what’s out there. So the Deno website maintains a list of compatible third-party modules. As of writing, there are 707 modules in the list.

The difference of Security between Deno vs Node

Perhaps one of Deno’s most touted improvements over Node is the permissions system. Let’s look at why.

Node.js

The Node runtime is very permissive, allowing code full access to the computer’s network and file system. There’s the potential for third-party code to wreak havoc on your system if unchecked.

Deno

Improving the security model is something that Ryan Dahl specifically set out to do when designing Deno. By default, all code is executed in a secure sandbox environment. This prevents code from having access to the file system, network, and environment variables unless access granted with a command-line argument.

# Allow script to make network requests
deno run --allow-net server.ts

Even better, when allowing read or write access to the file system, or access to the network, you can supply a whitelist. This means you could restrict a Deno program’s read/write access to the project’s data folder, for example, limiting any potential malicious damage.

Wrapping up with Deno

Deno vs Node

Before we wrap up, we just wanted to talk about one more thing. If you take a browse through the tools section of the manual, you’ll notice that Deno provides us with some nice bonus features. So we bring Deno’s installation.!

Installation

Deno ships as a single executable with no dependencies. You can install it using the installers below, or download a release binary from the releases page.

Shell (Mac, Linux):

$curl -fsSL https://deno.land/x/install/install.sh | sh

PowerShell (Windows):

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

Homebrew (Mac):

$brew install deno

Chocolatey (Windows):

$choco install deno

Scoop (Windows):

$scoop install deno

Build and install from source using Cargo

$cargo install deno

See deno_install for more installation options.

Getting Started

Try running a simple program:

$deno run https://deno.land/std/examples/welcome.ts

Or a more complex one:

import { serve } from "https://deno.land/std@0.65.0/http/server.ts";const s = serve({ port: 8000 });console.log("http://localhost:8000/");for await (const req of s) {  req.respond({ body: "Hello World\n" });}

You can find a more in depth introduction, examples, and environment setup guides in the manual.

Runtime Documentation

You can find the basic runtime documentation for Deno on doc.deno.land.

To clarify, Deno comes with a manual which contains more in depth explanations about the more complex functions of the runtime. Moreover, it has an introduction to the concepts building Deno, details about the internals of Deno, how to embed Deno in your own application and how to extend Deno using Rust plugins.

The manual also contains information about the built in tools that Deno provides.

Next to the Deno runtime, Deno also provides a list of audited standard modules reviewed by the core Deno team and guaranteed to work with a specific Deno version. These live alongside the Deno source code in the denoland/deno repository.

These standard modules are hosted at deno.land/std and are distributed via URLs like all other ES modules that are compatible with Deno.

To sum up with Deno vs Node

Above all, this article above is an overview of main differences between Deno vs Node that you need to know. Then, ArrowHiTech hopes this brings you as much useful information as you want. If you want to widen your knowledge not only in this topic but also many other ones, let’s visit our BLOG where we always update the most useful and valuable knowledge. Not only that, you can freely CONTACT US if you have any problems.

Tags

Share