Why Use Deno Instead of Nodejs For Your Next Project

see if Deno will replace node

Lately, Deno has been a buzz in the air, and it all started back in 2018, when Ryan Dahl creator of Nodejs, did a presentation where he talk about 10 things he regrets about Nodejs.

Moving forward to 2020 May, Deno — A “secure” runtime for JavaScript and TypeScript — was released version 1.0.

However, Nodejs has become almost a solid backbone for JavaScript stacks as a JS runtime environment.

In fact, whenever you see the letter “N” in any tech stack, such as MERN, MEAN, and MEVN, you will not be wrong to say that it stood for Nodejs.

Here’s is the thing, there is no perfect development ecosystem. Well, as time went on, developers started having some issues with Nodejs.

We have seen projects, frameworks, and web solutions trying to address a problem in another framework or so and they end up bring up another problem.

If you’re curious as an experienced developer or as a self-starter who is learning JavaScript, it will be very exciting to get started on Deno, since it is new.

In this article, we’ll show what makes Deno special, figure out if Deno is right for you, and walk you through starting your first Deno script.

What Makes Deno Special?

Deno is a JavaScript server language built for TypeScript and designed to improve on the shortcomings that exist in Node.

Let’s get some things clear, in case you don’t know what TypeScript is, it is a superset of JavaScript.

The striking difference between the two is that TypeScript is strongly typed – you are forced to define your data type creating variables.

The ease of JavaScript loosely-typed nature may be appealing, however, it is easier to pick out bugs in a strongly-typed language.

If this is confusing, just think of TypeScript as a strongly-typed JavaScript.

Out of the box, Deno provides support for direct module imports via URL, same as how web browsers will import JS libraries, fonts and other external resources.

This removes the need of package.json and node_modules which is just a list of third-party modules that an app will need to run. The end result is a much smaller app footprint and easier version control done through directory structure by the module’s administrator.

Third on the list, Deno gives security a priority. Access to network,  the file system, and environment are not readily available — the exact opposite of node — and require permission to access or use.

Is Deno Right for Me?

Looking at the core design principles with which Deno was built (TypeScript, security, module management), if those features are top on your priority, then you will need to invest some time and energy into Deno.

However, the challenge is that Deno is an infant (v 1.0.0). So, it’s definitely not the right route to take, if you’re looking to move production projects today.

Alright, Let’s say you’re new to JavaScript runtime environment, and want to learn something brand new, or maybe you are intimidated by the ocean of existing Node.js content, then let’s check out Deno together.

How Do You Get Started?

First, you will have to install Deno. This step will be different, depending on the operating system you use and your preferred method of installation.

For Linux users, you can simply install Deno using Shell

curl -fsSL https://deno.land/x/install/install.sh | sh

If you are an OSX users, you can use the method for Linux above or enter this below

brew install deno

iwr https://deno.land/x/install/install.ps1 -useb | iex

When you are done with installation, type deno –help for a list of resources.

see if Deno will replace node

Now that we’ve installed Deno, we’re going to begin with a very simple server app that illustrates all the 3 Deno’s core features.

Your First Deno Server

To start with, create a new file called server.ts, then add the first line:

import { serve } from "https://deno.land/std@v0.50.0/http/server.ts";

This line imports the serve module from a URI resource, something that you would have used npm install if you write this is Node.

Next, you will create your server and set it to listen to port 8000.

const server = serve({ port: 8000 });

With the serve() function, you will pass an object with the key-value pair port and 8000. This is stored in a constant named server.

Finally, with an asynchronous loop using for await…of, you will create a message which will dictate what happens when a request hits your server.

const message:string = "Hurray! First Server In Deno";
for await (const req of server) {
    req.respond({ body: message});
}
See if deno can take out node - server.ts

Each request that hits your server will be assigned the variable req where you have set the response body to your message variable. I guess you’ve noticed the “:string” after the constant’s name.

That’s a typical example of type setting, which is TypeScript’s core component.

How to Get Your Server Running

Now you’re through with writing the code, you will have to start it up.

In your terminal, start the server by entering:

deno run --allow-net server.ts.

If you observe closely we had to specify a –allow-net flag in the run command.

Just in case you omit that flag you’ll be greeted with a PermissionDenied error. You won’t see anything else in the terminal (because we didn’t write anything) but rest assured our test server is up and running. In your browser, navigate to localhost:8000 and see for yourself!

Conclusion

To sum it up, Deno as a rising JavaScript runtime environment was designed due to the various shortcomings in Node.js.

With a security-first mindset and decentralized package control.

Deno natively supports TypeScript which is a statically-typed superset of JavaScript.


Have you started following Deno or tried it out since v 1.0.0 was released? Please share your experiences, questions, and feedback below.

Leave a Reply

Your email address will not be published. Required fields are marked *