Deno says Hello World

As we know Deno made his official entry in May 2020, it is created by Ryan Dahl which is also the creator of Node, Deno is the new modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

But, why Deno?

Let’s start from the beginning – Ryan gave a talk on “10 Things I Regret About Node.js” in JSConf EU 2018 where he mentioned about his regrets while the initial design of Nodejs like not using Promises, using node_modules and package.json, leaving out file extensions. He eventually unveiled Deno, back then it was just a small & experimental project for Ryan which was an improved and secure version of node.

Two years later, in May 2020 Deno made his official release with Deno 1.0. A new Javascript runtime for built on Google V8 Engine, written in Rust, based on the Tokio platform.

What’s new in Deno than node?

Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

It is built with:

  • Rust (Deno’s core was written in Rust, Node’s in C++))

  • TypeScript

  • Tokio (the event loop written in Rust)

  • V8 (Google’s JavaScript runtime used in Chrome and Node, among others)

So let’s see what Deno have to offer.

Security

Secure by default. No file, network, or environment access, unless explicitly enabled

By Default Nodejs allows everything by default where you can write to any file, access environment variable, make network requests and so and on. It will open up a lot of security risk for the npm packages you are using, they will have access to all of it by default.

So instead of all access, Deno uses command-line flags to enable the type of permissions you need

For instance, if we want to write to a JSON file inside /tmp directory

					

import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts";

writeJson("/tmp/data.json", { foo: "bar" }, { spaces: 2 }); // returns a promise
writeJsonSync("/tmp/data.json", { foo: "bar" }, { replacer: ["foo"] });

Run the above code using this

The moment you run this code, you will get this Permission error because you don’t have access to /tmp directory.

File Permissions

For running the above code you have to pass the command line option of –allow-write with the name of the folder

					

deno run --allow-write=/tmp index.ts 

There are multiple command-line options available for handling a different set of permissions, you can read about all of the here

No more node_modules

This one has been one of the most significant change since the entire JS community relies and been using this and relying on node_modules be Node, React or Angular we all use   node_module.

Then what’s there for maintaining our dependencies.

well, now you can easily import modules using the URL on the web or a CDN like pika.dev or jspm. eg.

					

import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts"; 

Using Deno packages are distributed, we can use them by using the URLs without having any central registry, which Ryan explained here. Just Decentralization and no more node_modules and package.json.

Deno also presents import map so we don’t have to write URLs each time, some standard modules by Deno team which can be viewed here and imported by the same URLs.

TypeScript support out of the box

Deno makes it easy to use Typescript without using any modules or any configurations, it is also possible to write code in plain javascript which can be executed by Deno without any hassle.

Top-level Await

Top-level await enables you to use the consume await keyword without using the async keyword.

Old Behaviour

					

await Promise.resolve(console.log('Hello World from Deno'));
// → SyntaxError: await is only valid in async function 

New Behaviour

					

(async function() {
  await Promise.resolve(console.log('Hello world from Deno'));
  // → Hello world from Deno
}());  

Built-in Testing

Deno comes with a built-in test runner that you can use for testing your typescript or javascript without actually using modules like Mocha

Write a Test

To define a test, just use Deno.test with the name of the test and the module to be tested.

					

// Simple name and function, compact form, but not configurable
Deno.test("hello world #1", () => {
  const x = 1 + 2;
  assertEquals(x, 3);
});  

Running Tests

To run the test, just call deno test with the file that contains your test modules.

					

# Run all tests in the current directly and all sub-directories
deno test

# Run all tests in the specs directory
deno test specs/  

Deno shares many other features eg file watcher(which can be used as an alternative to nodemon), shell utilities, Debugger and many more.

Conclusion – is DENO going to replace Node?

We don’t think so, at least for a few years, to be honest. Nodejs is far from dying and it’s driven and used by a vast community and Deno is brand new which is the outcome of 2 years of Ryan’s idea where Node is been from the past 10 years. Deno isn’t been tested properly on real-world production scenarios and border cases, hopefully, in future, we hear back from the community on this.

And we are done 🙂

Hope you enjoyed reading and learned something out of it, feel free to add your questions or thoughts in the comments.