DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,864 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How to learn Deno as an alternative to Node.js
Bryant Son
Bryant Son

Posted on

How to learn Deno as an alternative to Node.js

Deno is a simple, modern, and secure runtime for JavaScript and TypeScript. It uses the JavaScript and WebAssembly engine V8 and is built in Rust. The project, open source under an MIT License, was created by Ryan Dahl, the developer who created Node.js.

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

Deno's GitHub repository 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:
import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";
Enter fullscreen mode Exit fullscreen mode

Install Deno

Deno's website has installation instructions for various operating systems, and its complete source code is available in its GitHub repo. I run macOS, so I can install Deno with HomeBrew:

$ brew install deno
Enter fullscreen mode Exit fullscreen mode

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

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

Run Deno

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

$ deno run https://deno.land/std/examples/welcome.ts
Enter fullscreen mode Exit fullscreen mode

If you explore the welcome example, 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:

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" });
}
Enter fullscreen mode Exit fullscreen mode

Save the file with a .tx extension. Run it with:

$ deno run --allow-net <name-of-your-first-deno-file.ts>
Enter fullscreen mode Exit fullscreen mode

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!"

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›