Meet Deno: The New JavaScript/TypeScript Runtime
Tags: deno, javascript, typescript
Published: 2020-11-01 18:18:39

<figure>

![Photo by Frank P. via Pixabay - A picture of the long-necked Brachiosaurus](https://dev-to-uploads.s3.amazonaws.com/i/fmwmtv0tp1e4m8f2iw0t.jpeg)

<figcaption> 
Photo by <a href="https://pixabay.com/users/frank_p_ajj74-1589101"> Frank P.</a> via 
<a href="https://pixabay.com/photos/dinosaur-prehistoric-times-lizards-4475295"> Pixabay</a>
</figcaption>

</figure>


> ####_Deno 1.0 was released on May 13, 2020_

[Deno](https://deno.land/) is [Ryan Dahl's](https://github.com/ry) (yeah, you guessed it right, the guy who created [Node.js](https://nodejs.org/en/)) latest venture.

But it isn't just another [JavaScript Engine](https://en.wikipedia.org/wiki/JavaScript_engine). It also supports [TypeScript](https://www.typescriptlang.org/) - JavaScript's strictly typed cousin - out of the box.

## Installing Deno

On `macOS`, you can install Deno using [Homebrew](https://formulae.brew.sh/formula/deno#default) - the open-source software package manager for `macOS`:

```powershell
brew install deno
```

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

<figure>

![The output of the "brew install deno" command. Image Credit: Pratik Chaudhari (Author)](https://dev-to-uploads.s3.amazonaws.com/i/l6bkymfik9rlq7xqnltg.gif)

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

</figure>



On `Windows`, [Chocolately](https://chocolatey.org/) serves as an alternative to macOS's Homebrew:

```powershell
choco install deno
```


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

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

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

```powershell
deno --help
```

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

<figure>

![The output of executing the "deno" command on Terminal. Image Credit: Pratik Chaudhari (Author)](https://dev-to-uploads.s3.amazonaws.com/i/i7g5ktpegmb2to7qjz5n.png)

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

</figure>

## Hello, Deno!

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

```powershell
deno
```

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

```javascript
console.log('Hello, Deno!')
```

Here's what you'll see:

<figure>

![The output of console.log('Hello, Deno!'). Image Credit: Pratik Chaudhari (Author)](https://dev-to-uploads.s3.amazonaws.com/i/m8sccxjvyydo28lhtkdy.png)

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

</figure>

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](https://www.npmjs.com/), like so:

```powershell
npm i express
```

And then you import it into your code using this:

```javascript
require('express')
```

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](https://www.nano-editor.org/) editor and add the following code to it:

```typescript
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" });
}
```

Save this in a file named `server.ts`.

<figure>

![The code to create a web server in the nano editor. Image Credit: Pratik Chaudhari (Author)](https://dev-to-uploads.s3.amazonaws.com/i/ts8suaeyqpngemztvbcj.png)

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

</figure>

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

```powershell
deno run server.ts
```

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

But what we get instead is this:

<figure>

![Error after running the "deno run server.ts" command. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/yqh0gnmm45o6lnwiij54.png)

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

</figure>

What does the following error in the above screenshot mean?

```powershell
error: Uncaught PermissionDenied: network access to "0.0.0.0:8000", run again with the --allow-net flag
```

You see, you need to explicitly grant `Deno` the permission to access the network. This is one of the many [security features](https://deno.land/manual/getting_started/permissions) 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`

```powershell
deno run --allow-net server.ts
```

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

<figure>

![The web server running on port 8000. Image Credit: Pratik Chaudhari (Author)](https://dev-to-uploads.s3.amazonaws.com/i/yfts7zdbh6fk6zkvku39.png)

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

</figure>

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`.

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

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.