Open In App

Deno.js | Introduction

Introduction: DenoJS is a Secure runtime for JavaScript and TypeScript based on the V8 JavaScript Engine (developed by The Chromium Project, Google), Rust Programming Language and Tokio which is an asynchronous runtime for Rust. NodeJS is also a JavaScript runtime which uses the V8 engine. DenoJS 1.0.0 was released on May 13th, 2020 and is created by Ryan Dahl, who is also the creator for NodeJS.

DenoJS aims to be a productive and secure scripting environment to provide an easy experience for the modern age developers. The creators of NodeJS had expressed several concerns over the functioning of NodeJS. They had expressed concerns over the security of Node, how Node handled packages and other legacy APIs within Node which will never change, among other things. Node was released in 2009 and since then JavaScript has changed a lot. They wanted to make a better version of NodeJS with modern JavaScript tools and APIs. They also wanted something compatible with the Browser and Server Environments and hence came the realization of DenoJS.



Advantages and Features of DenoJS: 

https://deno.land/x/[Package_Name]
import { serve } from "https://deno.land/x/http/server.ts"

This Import is used to create a simple HTTP Server in Deno. Another Key feature of Deno is that after this module is imported, it is cached to the Hard drive on load. Remote code which is fetched will be cached to the Hard drive when it is first executed, and it will never be updated until the code is run with the –reload flag. According to official DenoJS documentation, modules and files loaded from remote URLs are intended to be immutable and cacheable.



Installation of DenoJS: For the different ways on Installing DenoJS, Refer to this link. We will be Installing Deno using Chocolatey for Windows. For Installing Deno using Chocolatey, run the following command:  

choco install deno

This will install Deno on the local System to the default $HOME/.deno directory. This is the default Deno’s Base directory and is referenced via the environmental variable DENO_DIR
 

Getting Started: To check DenoJS Installation, run the command: 

deno --version

This should provide you with the version of TypeScript, V8 Engine and Deno. 

If we simply execute the command: 

deno

It runs the command: 

deno repl

Which opens up the REPL interface that stands for READ EVAL PRINT LOOP which means that we can type in basic JavaScript code from within the Command-line and it will compile, executes and prints the result. 

We will execute a simple Deno Script in our local, located at the standard deno.land/std/ URL. We can directly run this Script from the remote URL by using the Command:  

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

This is the link to the welcome.ts script in the official Deno docs Examples. We can view the Source code by simply navigating to that URL. The deno run command compiles the Script and executes the Script to display the result in your console. Deno Automatically knows whether we are running this script via the URL or just viewing it in the Browser.

To install the script on the local machine, we can run the command: 

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

This will install the welcome.ts script to the $Home/.deno/bin directory. The file will be downloaded as a .cmd file on Windows. In case the Installation already Exists, we need to explicitly overwrite it. 

 

Note: Run CMD with Administrator Privileges to avoid unnecessary errors with Deno.
We will create a welcome.ts file on the local machine and execute it with Deno. 
welcome.ts 




console.log("Welcome to GeeksForGeeks");

To execute this file, Run the command: 

deno run welcome.ts

Output:  

 


Article Tags :