Try Deno as an alternative to Node.js

Deno is a secure runtime for JavaScript and TypeScript.
72 readers like this.
Tips and gears turning

opensource.com

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.

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

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

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

Run Deno

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

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

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

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

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

That's it! You can learn more about Deno in this video I recorded.

What do you think about Deno? Please share your feedback in the comments.

What to read next
Tags
User profile image.
Bryant Jimin Son is an Octocat, which not official title but likes to be called that way, at GitHub, a company widely known for hosting most open source projects in the world. At work, he is exploring different git technology, GitHub Actions, GitHub security, etc. Previously, he was a Senior Consultant at Red Hat, a technology company known for its Linux server and opensource contributions.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.