DEV Community

Pratik Chaudhari
Pratik Chaudhari

Posted on • Originally published at Medium

Meet Deno: The New JavaScript/TypeScript Runtime

Photo by Frank P. via Pixabay - A picture of the long-necked Brachiosaurus

Photo by Frank P. via Pixabay

Deno 1.0 was released on May 13, 2020

Deno is Ryan Dahl's (yeah, you guessed it right, the guy who created Node.js) latest venture.

But it isn't just another JavaScript Engine. It also supports TypeScript - JavaScript's strictly typed cousin - out of the box.

Installing Deno

On macOS, you can install Deno using Homebrew - the open-source software package manager for macOS:

brew install deno
Enter fullscreen mode Exit fullscreen mode

Here's a GIF to give you a better idea:

The output of the "brew install deno" command. Image Credit: Pratik Chaudhari (Author)

The output of the "brew install deno" command. Image Credit: Pratik Chaudhari (Author)

On Windows, Chocolately serves as an alternative to macOS's Homebrew:

choco install deno
Enter fullscreen mode Exit fullscreen mode

On Linux, good ol' curl will do the job:

curl -fsSL https://deno.land/x/install/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

Once Deno is installed, one can run the following command to see all the command-line options that are available:

deno --help
Enter fullscreen mode Exit fullscreen mode

The output of the above command will look something like below:

The output of executing the "deno" command on Terminal. Image Credit: Pratik Chaudhari (Author)

The output of executing the "deno" command on Terminal. Image Credit: Pratik Chaudhari (Author)

Hello, Deno!

You can run Deno in REPL (Read-Eval-Print Loop) mode by simply executing the following command in the Terminal/Powershell/Shell:

deno
Enter fullscreen mode Exit fullscreen mode

Once the REPL is active, type the following code on the prompt and press the Enter/Return key:

console.log('Hello, Deno!')
Enter fullscreen mode Exit fullscreen mode

Here's what you'll see:

The output of console.log('Hello, Deno!'). Image Credit: Pratik Chaudhari (Author)

The output of console.log('Hello, Deno!'). Image Credit: Pratik Chaudhari (Author)

This is all good and fine. But it's just like all other "Hello World!" programs out there!

Let's have some real fun.

Let's see if we can build a web server in Deno!

Using third-party/external packages

In Node.js, if you want to (or rather, need to) use a third-party library in your code, you first need to install it using npm, like so:

npm i express
Enter fullscreen mode Exit fullscreen mode

And then you import it into your code using this:

require('express')
Enter fullscreen mode Exit fullscreen mode

But Deno allows you to import such packages directly. You just need to specify the package's URL in your code. Let's see how:

Fire up the nano editor and add the following code to it:

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

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

console.log("http://localhost:8000/");

for await (const req of s) {
    req.respond({ body: "Hello World\n" });
}
Enter fullscreen mode Exit fullscreen mode

Save this in a file named server.ts.

The code to create a web server in the nano editor. Image Credit: Pratik Chaudhari (Author)

The code to create a web server in the nano editor. Image Credit: Pratik Chaudhari (Author)

Exit the nano editor or open a new terminal and execute the following command:

deno run server.ts
Enter fullscreen mode Exit fullscreen mode

With the above command, the server should start listening over port 8000.

But what we get instead is this:

Error after running the "deno run server.ts" command. Image Credit: Pratik Chaudhari.

Error after running the "deno run server.ts" command. Image Credit: Pratik Chaudhari.

What does the following error in the above screenshot mean?

error: Uncaught PermissionDenied: network access to "0.0.0.0:8000", run again with the --allow-net flag
Enter fullscreen mode Exit fullscreen mode

You see, you need to explicitly grant Deno the permission to access the network. This is one of the many security features that are built into Deno (that's why they call it a secure runtime).

You can allow Deno to access the network by specifying the command line flag --allow-net

deno run --allow-net server.ts
Enter fullscreen mode Exit fullscreen mode

And voila! The server starts listening for requests on port 8000.

The web server running on port 8000. Image Credit: Pratik Chaudhari (Author)

The web server running on port 8000. Image Credit: Pratik Chaudhari (Author)

If you look at the following line of code, you'll notice that we have specified the full URL of the server.ts file which contains the serve() function that we use in our code to listen & serve requests on port 8000.

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

We didn't have to install it using npm beforehand. Deno automatically downloads it while executing the above code.

Deno also downloads any other packages that might be required by server.ts i.e., it also takes care of the transitive dependencies.

This saves us the trouble of executing npm install before running our programs.

This is something that's new and unique to Deno (apart from the need for explicit permissions, that we saw before) and makes it stand apart from Node.js.

Closing Comments

So far, Deno seems to be a good alternative to Node.js.

But will it be able to take its place someday?

Only time will tell.

Top comments (0)