An Introduction to Deno

Pankaj Baagwan
ducktyp’d
Published in
4 min readJun 1, 2020

A secure runtime for JavaScript and TypeScript. — by Deno.land

Since you are reading this blog, I would assume that you may have probably worked using Node.JS or maybe coming from Javascript development background.

Before Node.JS, Javascript world was somewhat unorganized, the only bigger movement we had were jQuery and Backbone (though there are some other smaller movement worth mentioning like Prototype.JS, etc)

Node.JS was a big thing that happened to Javascript community, it pushed it toward an organized community like other programming languages. But before Node.JS, most of developer had programming language as their core development work. We rarely used to find a developer who used to work in Javascript full-time.

Node.JS changed that scenario, people started taking Javascript seriously and that improved Javascript significantly and we have seen Javascript ecosystem evolved with Webpack, ReactJS, Angular, Vue.JS in front-end and Express.JS in the backend. But since then it had a lot of hiccups. We all can recall callback hell, node_modules being heavy and npm modules with unnecessary dependencies and code. This made backend development as a nightmare and that’s why nowadays you mostly see Node mostly being used as ecosystem manager rather than a complete server-side JS ecosystem as imagined initially.

To overcome these hurdles Ryan Dahl came with reimagining server-side JS with Deno. Here are few things that may catch your attention and help you differentiate Deno from NodeJS

  • Deno ships only in a single executable file, making it compact and without unnecessary overhead
  • It is secure by default, sandboxes your code as it should be. Files, Network and environment access needs to be enabled explicitly
  • Supports TypeScript out of the box, meaning that you can execute .ts files without first transpiling them to Javascript
  • No NPM or it's equivalent, no node_module overheads, modules as single JS file can be referenced as URL or file paths
  • No package.json and default index.js file for modules
  • All async operations on Deno return a promise
  • Deno always dies on uncaught errors
  • Uses ES Module to import no require
  • Remote files are always cached on the first execution and are not updated unless run with --reload flag, no npm install
  • Browser compatible by default, no more globals :), you can use window instead

So far so good, going by specs it does look promising and we are moving back to old Javascript simplicity, security while catching with the world of technologies.

Installation

Deno ships as a single executable with no dependencies. You can install it using your favorite installers, or download it 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

To check installation one could run

$ deno --version
# This should print following
deno 1.0.3
v8 8.4.300
typescript 3.9.2

First Program

Once you have installed Deno, please run post-install instructions if any, or you are good to go. Source your bashrc or bash_profile or restart your terminal. Run the following command

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

you should see something like this

Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using master branch https://deno.land/std/examples/welcome.ts
Compile https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕

Noticeable Features

Before we dive into notable features of Deno, we should ask ourselves, why we needed it? To make something more useful, we take it and improve upon it. V8 Engine is a remarkable sandboxed, high-performance JS engine that happened to us. Deno is kind of a v8 superset that adds features and server-side accessibility without overriding what it already provides

1.) No to Centralised package manager

In browsers, if we need a JS library, we just source it from a URL, it could be hosted anywhere on the web, we never restricted it to some centralized server. Deno allows us to fetch a library by its URL and cache it locally. So in Deno, the following import statement provided by ECMAScript2015, is correct

import { createRequire } from "https://deno.land/std/node/module.ts";

In addition to JS files we can also execute Typescript files, so here we are adding a feature to import , without restricting its original use

Deno is web compatible, since its a superset, so following command runs smoothly

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

2.) Sandboxed by default

V8 Engine is sandboxed, it restricts access to the underlying system for users. By default Deno implements the same making it more secure. So to leverage say network, it needs to have — allow-net flags. Consider the following

Create a file, say server.ts and paste following

import { serve } from "https://deno.land/std@0.55.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" });
}

To run the above code successfully, we need to run it with --allow-net flag otherwise Deno would complain

error: Uncaught PermissionDenied: network access to "0.0.0.0:8000", run again with the --allow-net flag

To run it with --alow-net simply run

$ deno run --allow-net ./server.ts

And then visit http://localhost:8000

3.) Browser Compatible

Whatever that works in the browser, works in Deno, no more globals , just use window and document , similarly API’s are also available

const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");const json = response.json();
const data = await json;
console.log(data)

Save code above in fetch.ts and run with

deno run --allow-net ./fetch.ts

That should produce, something like following

{
userId: 1,
id: 1,
title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
body: "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas..."
}

You are good to go now. Keep watching this space for more updates. Thanks for reading

--

--

Pankaj Baagwan
ducktyp’d

Architect, Tech Innovator, Certified Ethical Hacker and Cyber Security Enthusiast