How to learn Deno as an alternative to Node.js
Published: 2022-10-03 13:19:46

[**Deno**](https://deno.land/) is a simple, modern, and secure runtime for JavaScript and TypeScript. It uses the JavaScript and WebAssembly engine [V8](https://v8.dev/) and is built in [Rust](https://opensource.com/article/20/12/rust). The project, open source under an MIT License, was created by [ **Ryan Dahl**](https://en.wikipedia.org/wiki/Ryan_Dahl), the developer who created Node.js.

You can watch all of these videos and instructions in following tutorials:

{% embed https://youtu.be/vSJn8LB_h8k %}


{% embed https://youtu.be/sRxDGMow7j8 %}


{% embed https://youtu.be/BAAWtWkijSM %}


{% embed https://youtu.be/rUiSsE2hJWc %}

[Deno's GitHub repository](https://github.com/denoland/deno/blob/v1.5.4/docs/introduction.md) outlines its goals:

- Only ship a single executable (deno)
- Provide secure defaults
- Unless specifically allowed, scripts can't access files, the environment, or the network.
- Browser compatible: The subset of Deno programs which are written completely in JavaScript and do not use the global Deno namespace (or feature test for it), ought to also be able to be run in a modern web browser without change.
- Provide built-in tooling like unit testing, code formatting, and linting to improve developer experience.
- Does not leak V8 concepts into user land.
- Be able to serve HTTP efficiently

The repo also describes how Deno is different from NodeJS:

- Deno does not use npm.
- It uses modules referenced as URLs or file paths.
- Deno does not use package.json in its module resolution algorithm.
- All async actions in Deno return a promise. Thus Deno provides different APIs than Node.
- Deno requires explicit permissions for file, network, and environment access.
- Deno always dies on uncaught errors.
- Uses "ES Modules" and does not support require(). Third party modules are imported via URLs:

```javascript
import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";
```

## Install Deno

Deno's website has [installation instructions](https://deno.land/#installation) for various operating systems, and its complete source code is available in its [GitHub repo](https://github.com/denoland/deno). I run macOS, so I can install Deno with HomeBrew:

```bash
$ brew install deno
```

On Linux, you can download, read, and then run the install script from Deno's server:

```bash
$ curl -fsSL https://deno.land/x/install/install.sh
$ sh ./install.sh
```

## Run Deno

After installing Deno, the easiest way to run it is:

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

If you explore the [welcome example](https://deno.land/std/examples/welcome.ts), you should see a single line that prints "Welcome to Deno" with a dinosaur icon. Here is slightly a more complicated version that also can be found on the website:

```javascript
import { serve } from "https://deno.land/std@0.83.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 the file with a .tx extension. Run it with:

```bash
$ deno run --allow-net <name-of-your-first-deno-file.ts>
```

The _--allow-net_ flag might not be necessary, but you can use it if you see an error like error: _Uncaught PermissionDenied: network access to "0.0.0.0:8000.
_
Now, open a browser and visit localhost:8080. It should print "Hello, World!"