What is Deno? Will it Reeplace NodeJS - Node vs Deno Skip to content

What is Deno? Will it replace NodeJS – Node vs Deno

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

Installing Deno

  • Now click on install menu in the deno.land’s menubar
  • Now, based on Your OS , you can install by using listed commands.Suppose , you have Mac OS , So Copy the command as listed for Mac OS ,then open the terminal and pass the command you copy ,then press enter
  • Now ,  you have to set path ,So ,open your bash_profile by typing nano .bash_profile, then press ENTER , Then copy these two lines to your .bash_profile given below:
PastedGraphic5(2)

Now , Hit ctrl + X to save the .bash_profile

Now , You have successfully added the deno path

  • Now , You can verify your deno Installation by typing deno –version in the terminal to check your current deno’s version.
  • Also , you can type demo –help  in the terminal to check all the deno commands
  • Also, you can open the deno shell in the terminal by just typing deno and then just press enter.Then you can execute any demo’s Code there.

Now, Lets Start with first Deno’s Program

  • Now, Open VS Code , goto extension , then install deno plugin as shown :
  • Now, create a new folder named as Deno where we will write our first program.
  • Deno has a supports of JavaScript an TypeScript. So you can either write JS or TS. So, we will create a file named as one.ts

  • Now, lets write code in our one.ts file.This is our type script code :
  • Now ,  we have to run this code ,  so open terminal in vs code by right clicking on file (one.ts) , then navigate to terminal :
  • Now, terminal will be opened in the bottom of the VS Code.
  • Now, in order to run our code , type deno run one.ts(#your_file_name) , Then hit enter button,Then your program will be compiled in first line, and you will get output ion second line
  • Now , if you try to run your file again without changing your code , then it will not re-compile your code and will just return the result. Code will be only compiled if you made any change in your code

Features of Deno

1. Secure

Now, we will walkabout security features in the deno. So ,  Lets start with reading a file and we will see that How security works with a file.So, create a file named as readingfile.ts

PastedGraphic21(010)

Now, we will use some deno standard libraries in our code.Deno has its own standard libraries which comes from deno.land

Now we will use import (node js has require whereas deno has its own import statement).

It use ES (ECMA SCRIPT) module as a standard.So , deno will not going to use a common GS library. So,inside import  , we will just use readFileStr as shown :

         import {readFileStr}

So ,  it will read a file and will return a String.

Now, we have to specify that from where we will import readFileStr in our code , So we will use from(as deno doesn’t have package manager (NPM), so we can directly import using urls) followed by an URL.It will use URL’s and download modules from that URL’s.

Here , The URL from where readFileStr will be downloaded is  https://deno.land/std/fs/mode.ts’ .Where

std stands for standard

– fs stands for file system

-mode stands for modules and it is ts(TypeScript) file

Now , this dependency will be downloaded in our local machine  , and then will be cached in our local machine.

 

So , Now we will start with reading a file using readFileStr ,  and inside readFileStr 

– mention the file path which you want to read as shown

-specify the encoding scheme like UTF-8 as shown

It doesn’t use any callback mechanism like node JS .Here we will use Promises as we are using Promises based API’s

So , we will get promise in return. Here, there is no need of call backs as deno has promises based API’s

Now, we will check wether our promise is resolved or not

Here, We will check wether our promise is resolved or not. If it got resolved, we will get content. If it is not resolved, we will get an error.

Here is the code :

Formatting the code

Now , we have to format our code, so that it will look more pretty. For that type deno fmt readingfile.ts (yourfilename) in terminal , then hit enter.

Here , fmt stands for format and our readingfile.ts will get formatted. So, FMT is a tool present in deno used for formatting the code with proper alignments and indentations

OUTPUT :

Now, our code is formatted with perfect indentations as you can see in the output.

Now, lets run our code to Read the file

Enter command deno run readingfile.ts in terminal to run our readingfile.ts file, then our program will be compiled.

Now, when we are trying to compile our file, we got some errors.The reason is Deno & its API’s are currently unstable because deno is currently not production ready

So now we have to use unstable version , so we have run our file using deno run  –unstable follows by file name(readingfile.ts)

Now, we get an access denied error.It is secure. So it need permission to access our files. So we have to use  –allow-read flag as mentioned below to successfully compile and run our program.

So, our file has been read, and we got content of file in output.

2. Supports JS and TS

Deno has a support of JavaScript(JS) and TypeScript(TS), So you can write both JS and TS .

3. Build on Top of V8, RUST and Tokio

  • Its runtime environment is based on v8 engine.
  • Deno is written in RUST language.
  • built with Tokyo library for the event loop for asynchronous stuff.
  • It has standard modules that is reviewed, So these reviewed modules are available in Dual and STD.So they have provided so many standard modules like date-time , formatting , FS (file system) which we use in file handling, hash HTTP , IO Path etc.

So , these modules doesn’t have external dependency.If you want to check the standard deno modules, goto deno.land

Then go to standard library menu

Here, you will see all the standard libraries in the deno. Now, we will move towards next feature of deno

4. Built-In Tools like Testing , Formatting and Linting

Example : if we write a code and it is not formatted

Now, just type deno fmt filename to format it and hit enter

It has built-in tools for formatting , testing , bundle.

Here, Bundle  means that if your code has lot of dependencies , then if you bundle your code, your all dependencies will go inside a single bundle.So you can share the bundle and it will run as a independent file.

So, Let’s see how to create a bundle file :for creating a bundle file , you need to specify

     deno bundle <filename.ts> —unstable <bundleFilename>

And our read.ts bundle file will be generated as shown below :

Note: if you forget to specify bundleFilename,so it will print our bundle file o standard output

5. Promises based API & top level await

We have discussed earlier about Promises based API , now move towards top level await.

In ECMA8 , we have await option,  but it must be wrapped inside a function with a async keyword.But now , we have a top level await.So, lets understand top level await by using an example :

Now, lets create a file awaitDemo.ts

Now, I want to fetch some data from server using await feature .

  • Now, scroll down to Resources section of the page ,
  • Now paste that url in our code. That will be stored inside url variable as shown:
  • Now, we will use this url variable inside our fetch method to fetch data from that url along with await as shown :
  • Now it will return a response, so stored it inside response variable as shown :
  • Now, we will get our response, now we have to change this response into JSON object  and then we will print our json data:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Now, Lets compile our code

Goto terminal , then enter deno run awaitdemo.ts to run our await demo.ts file.

So, our code is compiled and an error is generated. Which is permission denied error because it is secure.so we need to run this again by specifying a —allow-net flag.

Now it is showing us Promise { <pending> } because whenever we are using response it is giving a promise, we have to use await where we convert response to JSON object.

OUTPUT :

6. Uses ES MODULE as Standard

Deno uses ES modules as default model system whereas Node js used common JS.

Let’s understand this with an example :

  • Create a folder named as math and inside math folder, create a file named as calc.js.
  • So, inside calc.js , simply create an object and inside that object create an add(a,b) method which will return addition of two numbers(a+b)

Now, we want to set a communication between two JS files

  • So create a new file named as caller.js which will be outside the math folder.
  • So, inside caller.js , we need to consume calc.js. we can do this by using export keyword to export the object we need to export.so , we want to export the object from calc.js , So, write export keyword before obj because we are using ES modules.
  • Now, once our object gets exported from calc.js , we can import it directly inside caller.js using import keyword by specifying object name which will be imported and location of the file from which object is imported in our file.
  • Now, we can use add function which is inside our imported object to add two numbers and display the result.

Output :

7. Deno is for Frontend and backend Development

Deno can be used in frontend as well as backend development. Deno has the support of Frontend Object as well.

So , its main goal is to be a browser compatible.It provides some built in objects. It should also provide support for window object and it has a support of fetch.

If you type window in the deno shell ,it shows that it has support of window as it contains all the window objects.

So , you can use it for frontend as well as for backend . Now, lets have a look on Deno’s history.

History of Deno

  • Deno was invented by Ryan Dahl

Ryan Dahl is the father of Node JS as well as Deno

  • Deno was initially announced on the JS conference 2018.and its first initial release was on 13th may. And its first stable release was on 3rd JUNE 2020.

Now, its time to see the difference between node and Deno

DENO VS NODE

Conclusion

For Node JS developer ,  Node JS is not going anywhere because Deno is a newbie for backend and frontend whereas NodeJS is mature and have a very big  ecosystem.It has NPM and have lacks of Modules.

So , now its very hard to say that Deno can replace Node Js because it is currently under development and unstable.

Sign Up and Start Learning