Try Deno as an alternative to Node.js | Opensource.com

Try Deno as an alternative to Node.js

Deno is a secure runtime for JavaScript and TypeScript.

Tips and gears turning
Image by : 

opensource.com

x

Subscribe now

Get the highlights in your inbox every week.

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

denoland.jpg

Deno GitHub repo

(Bryant Son, CC BY-SA 4.0)

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.

Computer screen with files or windows open

A breakdown of many JavaScript options for frameworks—their strengths and key features.
JavaScript in Vim

JavaScript is a super-popular language, as evidenced by the 10 best JavaScript articles from 2020.

Topics

About the author

Bryant Son - Bryant Jimin Son is a Senior Consultant at Red Hat, a technology company known for its Linux server and opensource contributions. At work, he is working on building the technology for clients leveraging the Red Hat technology stacks like BPM, PAM, Openshift, Ansible, and full stack development using Java, Spring Framework, AngularJS, Material design. Prior to joining Red Hat, Bryant was at Citi Group's Citi Cloud team, building the private Infrastructure as a Service (IaaS) cloud platform...