Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptDeno.js : The next step for JavaScript

Deno.js : The next step for JavaScript

The creator of Node js found there were many flaws in Node as can be shown in this video: https://www.youtube.com/watch?v=M3BM9TB-8yA. He started the project called deno 2 yrs back which was open source and was in alpha. Recently deno 1.0 (production-ready) was released on May 13, 2020.

Javascript has come a long way from a mess as a browser language to a server-side robust language that is used for the full-stack development of applications. But there were many flaws in JS that got into Node that were updated in the coming years but not got updated in Node. The creator of Node decided to build something like Node but without all these flaws. So now we have a new server-side tech.

Deno Features:

  1. Modern features of javascript like promises etc.
  2. Full typescript support, natively compiled. Hence you can take advantage of features like IntelliSense.
  3. Extensive standard library.
  4. It has the standard ES modules compatibility.
  5. No package manager, modules are imported via URL.
  6. First-class await support.
  7. Built-in testing functionality.
  8. Browser compatible whereas Node was just for the backend. ( classes like window can be accessed )

Will it replace Node js?

No, Node is now a well-established technology and it is being used extensively worldwide. But people will start to move towards deno as it is essentially Node but better.

Similarities between Node and deno 
 

  • Both are developed upon the V8 chromium engine.
  • Both are great for server-side JavaScript.

Differences 
 

  • Node is written with c++and js while deno is made with rust and typescript.
  • Node has a package manager called npm while in deno you import ES modules from URLs.
  • Node uses CommonJS syntax while deno uses official ES modules syntax.
  • Deno uses Ecmascript features in APIs and libraries while Node uses callbacks.
  • Deno has great security features while Node allows access to anything.
  • Deno is trying to be compiled in a binary. ( but this isn’t a production-ready feature )

Promises

Node was implemented before JS had Promises or async / await. Node’s counterpart to promises was the EventEmitter, which APIs in Node are based upon.
While All callbacks in Deno arise from promises.

The Deno Sandbox

Deno creates a sandbox environment that only allows system calls that are allowed explicitly by its CLI.
There is nothing stopping Node from accessing your private files on disk and sending it to the server while Deno tries to replicate the same permission model that the browser implements.
If a program needs to access the network we have to give it the permission explicitly like this:
 

deno run --allow-net app.ts

First Class typescript support

Deno supports TypeScript without additional tooling. The runtime is designed with TypeScript in mind. The deno types command provides type declarations for everything provided by Deno. Deno’s standard modules are all written in TypeScript.

Compatibility

Deno is not compatible with existing JS tooling and Node packages. But efforts are being done to make the tooling more compatible.

Formatting Code

You can easily format the code based on standards for easy readability by 

Deno fmt app.ts

Should you learn Deno?

You should start with JavaScript and then learn Node and then only should start deno as the fact is that the companies using Node will not replace it with deno easily because it requires enormous resources.
Node will be used extensively in the industry. So the first step should always be to learn Node and then deno for the new clients trying to implement it.

The Standard library
 

  • Archive : tar archive utilities.
  • Async : async utilities.
  • Bytes : helpers to manipulate bytes slices.
  • Log : logging utilities.
  • Mime : support for multipart data.
  • Datetime: date/time parsing.
  • Encoding :  encoding/decoding for various formats.
  • Flags : parse command-line flags.
  • Fmt : formatting and printing.
  • Node : Node.js compatibility layer.
  • Path : path manipulation.
  • Ws : WebSockets.
  • Fs : file system API.
  • Hash : crypto lib.
  • Http : HTTP server.
  • Io : I/O lib.

Let’s get our hands dirty with deno:

Installation

Using Shell (macOS, Linux): 
$ curl -fsSL https://deno.land/x/install/install.sh | sh

Using PowerShell (Windows): 
$ iwr https://deno.land/x/install/install.ps1 -useb | iex

Using Homebrew (macOS): 
$ brew install deno

Using Chocolatey (Windows): 
$ choco install deno

Using Scoop (Windows): 
$ scoop install deno

Getting Started

Try running a simple program:

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

Or a more complex one:
 

javascript




import { serve } from
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
 req.respond({ body: "Hello World\n" });
}


You can use this command to get the manual for deno CLI

$ deno –help:

deno 0.42.0
A secure JavaScript and TypeScript runtime

Docs: https://deno.land/std/manual.md
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues

To start the REPL, supply no arguments:
 deno

To execute a script:
 deno run https://deno.land/std/examples/welcome.ts
 deno https://deno.land/std/examples/welcome.ts

To evaluate code in the shell:
 deno eval "console.log(30933 + 404)"

Run 'deno help run' for 'run'-specific flags.

USAGE:
   deno [OPTIONS] [SUBCOMMAND]

OPTIONS:
   -h, --help
           Prints help information

   -L, --log-level <log-level>
           Set log level [possible values: debug, info]

   -q, --quiet
           Suppress diagnostic output
           By default, subcommands print human-readable
            diagnostic messages to stderr.
           If the flag is set, restrict these messages to errors.
   -V, --version
           Prints version information


SUBCOMMANDS:
   bundle      Bundle module and dependencies into single file
   cache       Cache the dependencies
   completions    Generate shell completions
   doc         Show documentation for a module
   eval        Eval script
   fmt         Format source files
   help        Prints this message or the help 
                of the given subcommand(s)
   info        Show info about cache or info 
                related to source file
   install     Install script as an executable
   repl        Read Eval Print Loop
   run         Run a program given a filename 
               or url to the module
   test        Run tests
   types       Print runtime TypeScript declarations
   upgrade     Upgrade deno executable to newest version

ENVIRONMENT VARIABLES:
   DENO_DIR             Set deno's base directory 
                        (defaults to $HOME/.deno)
   DENO_INSTALL_ROOT    Set deno install's output directory
                        (defaults to $HOME/.deno/bin)
   NO_COLOR             Set to disable color
   HTTP_PROXY           Proxy address for HTTP requests
                        (module downloads, fetch)
   HTTPS_PROXY          Same but for HTTPS

RELATED ARTICLES

Most Popular

Recent Comments