Skip to main content

Deno - The easiest, most secure JavaScript runtime

· 4 min read
Bruno Carneiro
Fundador da @TautornTech

Deno

Introduction​

In 2020 I wrote an initial article covering some topics about Deno, its advantages and disadvantages. Since then the tool has evolved quite a bit, and with that I will bring some content covering development with Deno. In this article I will present some basic examples that demonstrate Deno's capabilities.

Installation​

Deno provides several installation options:

Mac & Linux

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

Windows

choco install deno

First Steps​

Deno is a JavaScript/TypeScript runtime that tries to be compatible with modern browsers, so a console.log can be run in Deno and in a browser.

function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}

function hello(name) {
return "Hello " + capitalize(name);
}

console.log(hello("john"));
console.log(hello("Sarah"));
console.log(hello("kai"));

hello-world.js

To run the file above type: deno run hello-world.js

The output will be:

Hello John
Hello Sarah
Hello Kai

The same file can be rewritten in TS, since Deno ships it natively, without the need for installation/configuration.

Dependencies​

Deno uses URLs for dependency management. A convention is that all dependency URLs are in the same place called deps.ts. Dependencies are exported to be used in other locations. And dev dependencies go in dev_deps.ts.

It is important to note that this is just a convention, but it can be done in whatever way is best for the project/development team.

Example to create a file to calculate the total cost of an operation​

First I will create the deps.ts file, exporting some functionality from the ramda lib, as below:

export {
add,
multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js"

Then I created the arithmetic.ts file importing add and multiply from the deps.ts file.

import { add, multiply } from "./deps.ts"

function totalCost(outbound: number, inbound: number, tax: number): number {
return multiply(add(outbound, inbound), tax);
}

console.log(totalCost(19, 31, 1.2));
console.log(totalCost(45, 27, 1.15));
Learn more at modules

The example above is quite simple: I pass 3 arguments — outbound, inbound, and tax — to calculate the total cost of an operation. Result:

/**
* Output
*
* 60
* 82.8
*/

Fetch Data​

Just like in browsers, Deno implements web standards, so it is possible to perform a fetch data using the fetch API.

/**
* Output: JSON Data
*/
const jsonResponse = await fetch("https://api.github.com/users/denoland");
const jsonData = await jsonResponse.json();
console.log(jsonData);

/**
* Output: HTML Data
*/
const textResponse = await fetch("https://deno.land/");
const textData = await textResponse.text();
console.log(textData);

/**
* Output: Error Message
*/
try {
await fetch("https://does.not.exist/");
} catch (error) {
console.log(error);
}

fetch.ts

Deno by default has a security layer for IO (input/output) control, making it inaccessible. Therefore, it is necessary to explicitly grant this access permission to the script. In this way, to run the script above it is necessary to grant a network permission --allow-net, so I can make the HTTP requests.

To learn more about Deno's permissions, visit Permissions

Conclusion​

As you can see, the learning curve for Deno is very low for those who already know JavaScript/TypeScript. Those who work with/know Node will notice countless similarities, but the big differences are mainly the security layer and the way packages are imported (without installation, without creating a node_modules).

Deno is very simple and easy to learn; this is just an initial article with some features and a first look at the tool. Of course, using it in production everything I listed here expands quite a bit and it is necessary to go deep into many other points that I haven't even mentioned here.

But for this initial article I just wanted to bring some examples and show how the tool works.

info

All the examples described here were taken from Deno's own documentation.

References​