Intro to Deno vs Node

Ďēv Šhãh 🥑 - Oct 14 - - Dev Community

This is an introduction blog on Deno. Since Deno 2.0 was just launched on 9th Oct 2024. Therefore, I decided to write a blog for all those who do not know it. I am excited to try Deno 2.0.

Introduction

In simple words, Deno serves the same purpose as Node but has more features than what Node offers. In fact, both technologies are developed by the same person, Ryan Dahl. Compared to Node, Deno takes a security-first approach, which is discussed further. Since Deno and Node serve the same purpose, to explain Deno’s features, it is mostly compared with Node.

Deno vs Node

Following are a few differences of Deno over Node:

1. Security

The first thing is security. As mentioned earlier, Deno takes a security-first approach. For instance, working with a file in Node is straightforward; you just import the fs module, write the script, and run it. However, with Deno, although the process is similar, the script won’t run unless a specific flag is passed while executing it.

2. TypeScript Support

The second amazing feature over Node is the built-in TypeScript support. In Node, to use TypeScript, you first have to set up a TypeScript project with tsconfig.json, and then transpile the TypeScript code to JavaScript before running it. However, with Deno, you just need to create a .ts file and run it directly.

3. No node_modules and Config Files

Deno does not require any package manager to manage the modules in the project. Following is how various modules are imported:

import { serve } from "https://deno.land/std/http/server.ts";
Enter fullscreen mode Exit fullscreen mode

This eliminates the requirement of the node_modules folder and any config file.

4. Support for Web APIs

The Fetch API is globally available in Deno, as opposed to in Node. In Node, you need to import the module to use the Fetch API to call an API; however, in Deno, you can directly use the Fetch method to call an API, just like in a browser.

For instance:

// Using the Fetch API to make a GET request
async function fetchData() {
    try {
        const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");

        // Check if the response is ok (status in the range 200-299)
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // Parse the response as JSON
        const data = await response.json();

        // Log the retrieved data
        console.log("Fetched data:", data);

    } catch (error) {
        console.error("Error fetching data:", error);
    }
}

// Call the function to fetch data
fetchData();

Enter fullscreen mode Exit fullscreen mode

5. Top-Level await Support

Deno allows the use of await anywhere in the file; it does not need to be wrapped in an async function.

This is how it works in Deno:

// Deno example using top-level await
const response = await fetch("https://api.example.com/data");
const data = await response.json();
console.log(data);
Enter fullscreen mode Exit fullscreen mode

And this is how it works in Node:

// Node.js example (pre top-level await support)
(async () => {
    const response = await fetch("https://api.example.com/data");
    const data = await response.json();
    console.log(data);
})();
Enter fullscreen mode Exit fullscreen mode

In Node, you can only use await inside an async function.

6. Built-in Testing Tools

Deno provides built-in testing tools, which means there is no need for additional setup or external libraries to install. In contrast, in Node, you need to choose and configure an external testing library.

Moreover, Deno 2.0 also has many new useful features to explore and assist in making programming fun and less complex.

Here is the Deno documentation to know more about it.

Final Words

Although Deno has a lot of cool features compared to Node, I feel it would still take time to be accepted by industries on a large scale since Node has been the trustworthy framework for the last few years and is well-known by companies as well as developers.

Let me know what you think about Deno.

Citation
I would like to acknowledge that I took help from ChatGPT to structure my blog and simplify content.

. . . . . . . . . . . . . . . .
Terabox Video Player