What it's all about
Deno is a simple, small, and secure runtime for JavaScript and TypeScript originally developed by Ryan Dahl - the creator of Node.js. Unlike Node.js, Deno "explicitly takes on the role of both runtime and package manager within a single executable". Watch Ryan Dahl announce Deno at JSConf back in 2018: 10 Things I Regret About Node.js
Node.js on the other hand, is perhaps the most popular server-side JavaScript runtime environment originally developed by - once again - Ryan Dahl.
So let's get into detail and compare these runtimes.
As always, all code samples are available in our public Github repo.
Install
As I mostly work on macOS, I will cover macOS aspects only - please refer to the platform docs for support of other operating systems.
Deno
brew install deno That's it - brew installs deno on your system.
Over time, you might need to change different versions of deno on your system. For that, there is dvm - the deno version manager. This small but very useful tool enables you to easily switch versions:
dvm install 1.0.0 dvm install 1.23.3 dvm use 1.23.3 Node.js
brew install nodejs Just to be fair - the same support is available for Node.js.
As for deno, there is a comparable feature available for Node.js (which, of course, is the older feature ;)
nvm use latest nvm install 14 nvm use 14 Getting started
Let's dive into the code - and beyond the classic Hello World. Both runtimes are best used for writing servers. So let's just do that.
deno
Deno shines with a new security concept: As a developer, you need to explicitly grant access to resources such as network, file access, etc. Let's see how this works by accessing a simple web API:
Typescriptfetch-deno.ts
const url = 'http://hn.algolia.com/api/v1/search?query=typescript';
fetch(url)
.then((result) => result.json())
.then((result) => console.log(result.hits)); So when you try to run this code with
deno run fetch-deno.ts you should see the following error in your terminal:
Deno denies network access by default - which is a great security improvement. When you type
deno run --allow-net fetch-deno.ts everything works fine, and you should see a JSON list of 20 items.
A second interesting aspect in this short code sample is:
Deno provides the same interface as the Browser API. This means that apps written for the Browser can be executed directly in Deno.
Let's dig a bit deeper and write a simple HTTP server in Deno.
deno-server.ts
import { serve } from "https://deno.land/std@0.149.0/http/server.ts";
function handler(req: Request): Response {
return new Response("Hello, World!");
}
serve(handler); This code reveals another unique feature of Deno:
Deno imports all external libraries with an absolute path. The code is loaded directly via URLs. This is a big difference from Node.js: You do not need to run npm install or anything comparable. Deno loads the external code libraries for the first run and caches it - a concept inspired by the Go language.
Node.js
To be fair, we need to compare the Node.js runtime by executing the same code as in Deno.
So let's start with a simple fetch:
fetch-node.ts
let url = 'http://hn.algolia.com/api/v1/search?query=typescript';
fetch(url)
.then((result) => result.json())
.then((result) => console.log(result.hits)); As this code uses a new feature, you need to ensure that you are running on the latest Node.js version (as of writing this article, it was 18.7.0).
When you now run:
node fetch-node.ts you should again see a JSON list containing 20 items.
Writing a simple HTTP server in Node.js is quite straightforward.
nodejs-server.ts
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
}); Wrap up
So when it comes to developer experience, Deno solves a couple of teething issues from Node - for me, the best feature is the much more lightweight dependency management. But Node.js still is the de-facto standard for backend software running JavaScript or TypeScript - with a significantly larger ecosystem regarding hosting and deployment, framework support, etc.
So the best you can currently do is - like with most new technologies - keep Deno on your watchlist and maybe give it a try for small to midsize projects. It's fast, secure, and provides a stellar developer experience. For large projects, I still recommend Node.js. For now. Let's recap in a couple of months ;)