What Is Deno? Is It A Replacement For Node.js?
Tags: node, deno, software
Published: 2020-07-01 07:59:01

If you have used Node.js, but don’t like its packet manager npm or you want a more secure JavaScript runtime environment than Node.js, then deno is there for you. 

**What Is Deno?**
Deno is a new runtime for JavaScript and TypeScript, created by Ryan Dahl- the creator of Node.js. It is intended to fix the design problems in Node.js. Like Node.js, Deno is essentially a shell around the Google V8 JavaScript engine, even though unlike Node.js it includes the TypeScript compiler in its executable image.

**Deno And Advanced JavaScript-**
According to Dahl, Javascript lacked some features that are useful for Node.js. Some of these are added to JavaScript over the years as a part of the ECMAScript (ES) standard. Javascript has callbacks and events forever, but it can make the code more complicated particularly when you need to chain asynchronous actions. Syntax is more readable with Promises. Promise is a returned object that  represents the eventual completion or failure of asynchronous operation, to which you can attach callbacks. Declaration of a function async simplifies the syntax and allow you to use await in the function to pause in non-blocking way until the promise settles.

At the time of Node.js creation, the de facto standard for JavaScript modules was CommonJS, which npm supports.  Afterward, ECMAScript committee officially support a different standard, ES Modules which jspm supports. ES Modules are supported by Deno.

TypeScript is a typed superset of JavaScript. It compiles to plain JavaScript. TypeScript adds optional types, classes and modules to JavaScript and supports tools for large-scale javascript applications. Deno includes an image of the TypeScript compiler as a part of its runtime. If you pass a TypeScript file to Deno, it will first compile it to JavaScript and after that pass to V8 engine.

**Features of Deno-**
*1. Security (permissions)-*
One of the most important features of Deno is- Focus on security. Deno executes the code in a sandbox, means runtime doesn’t have access to- File system, Network, Execution of other scripts, and the environment variables.

How does the permission system works?

(async () => {
 const encoder = new TextEncoder();
 const data = encoder.encode('Hello world\n');
 await Deno.writeFile('hello.txt', data);
 await Deno.writeFile('hello2.txt', data);
})();
According to the above example, script creates two files- hello.txt and hello2.txt with the message Hello world. The code has no access to the file system, as it is being executed in the sandbox. Deno namespace provides many fundamental helper functions. With the use of namespace, we are losing the browser compatibility.

When we run it with execution of- 

deno run write-hello.ts
We are notified with following warning-

⚠️Deno requests write access to "/Users/user/folder/hello.txt". Grant? [a/y/n/d (a = allow always, y = allow once, n = deny once, d = deny always)]
If we select the allow always option, we will get the access only at once. It we will select deny option, it will show the  PermissionDenied message and we will not have any error-handling logic.

Now, if we use the following command to execute the script- 

deno run --allow-write write-hello.ts
Both files are created and there are no prompts.

Beside the –allow-write flag for the file system, there are some other flags are available to enable network requests, access the environment and to run subprocesses and these are –allow-net, –allow-env, and –allow-run respectively.

*2. Modules-*
Know more at- [https://solaceinfotech.com/blog/what-is-deno-is-it-a-replacement-for-node-js/]