Get up and running quickly with Deno, a modern alternative to Node.js

I explore new projects every week, and it’s rare that one catches my attention as much as Deno did.

deno logo

What is Deno?

If you are familiar with Node.js, the popular server-side JavaScript ecosystem, then Deno is similar to Node but with several improvements.

Here are some of the features I like most about Deno:

  • It is based on modern features of the JavaScript language.
  • It has an extensive standard library.
  • It has TypeScript at its core, which brings a huge advantage in many different ways, including first-class TypeScript support.
  • It embraces ES modules.
  • It has no package manager.
  • It has a first-class await.
  • It has a built-in testing facility.
  • It aims to be browser-compatible by providing a built-in fetch and the global window object.

In this guide, we will explore all of these features.

After using Deno and experiencing its benefits, Node.js may start to feel outdated. The Node.js API is callback-based, which can be difficult to work with in comparison to the more modern approach of promises and async/await. Although Node.js is still an excellent choice, Deno’s first-class TypeScript support and modern standard library make it an attractive alternative that may see increased adoption over time.

Why Deno? Why Now?

Deno was announced almost 2 years ago by Ryan Dahl, the original creator of Node.js, at JSConf EU. In his talk, Ryan explained some of the regrets and shortcomings of his early decisions with Node.js. Technology evolves, and today JavaScript is a very different language from what it was in 2009 when Node.js was created.

The reason I am writing this guide now and not back then is because technologies need time to mature. Deno has finally reached stability with the release of Deno 1.0, which is a significant milestone. This means that there will not be major breaking changes until Deno 2.0, which is important when diving into a new technology.

Should you learn Deno?

Learning something new like Deno requires effort, so it’s natural to be cautious. If you are just starting out with server-side JavaScript and have never worked with Node.js or TypeScript before, I would recommend starting with Node.js. However, if you already love TypeScript and want to use await everywhere without depending on a large number of npm packages in your projects, then Deno might be the right choice for you.

Will Deno replace Node.js?

No, Node.js is a well-established technology with strong support and will continue to be widely used for many years. Deno is still a young project, but its first-class TypeScript support, modern standard library, and other features make it an attractive option for certain use cases.

First-Class TypeScript Support

One of the major advantages of Deno is its first-class TypeScript support. Deno is written in Rust and TypeScript, two rapidly growing programming languages. While you can write your code in plain JavaScript, leveraging the TypeScript features can bring numerous benefits. For example, with Deno and TypeScript, you can enjoy features like type checking and advanced IntelliSense in editors like Visual Studio Code, which greatly aids productivity.

Similarities and Differences with Node.js

Deno is meant to replace Node.js, so it’s helpful to compare the two.

Similarities:

  • Both are based on the V8 Chromium Engine.
  • Both are great for developing server-side applications with JavaScript.

Differences:

  • Node.js is written in C++ and JavaScript, while Deno is written in Rust and TypeScript.
  • Node.js has an official package manager called npm, while Deno does not. Deno allows you to import ES Modules from URLs.
  • Node.js uses the CommonJS syntax for importing packages, while Deno uses ES Modules.
  • Deno’s standard library and API are built using modern ECMAScript features, while Node.js uses a callback-based standard library and has no plans to upgrade it.
  • Deno offers a sandbox security layer through permissions, which restricts access to resources based on the permissions granted by the user. In contrast, a Node.js program has access to everything the user has access to.
  • Deno has plans to compile programs into executables without external dependencies, similar to Go, although this feature is not available yet.

No Package Manager

Deno does not have a package manager like npm or yarn. Instead, it allows you to import packages directly from URLs. This flexibility offers some advantages, such as the ability to create and use packages without having to publish them on a repository like npm. However, there is currently no official package manager for Deno.

You can find third-party packages on the Deno website at deno.land/x/.

Install Deno

To install Deno, the easiest way is to use Homebrew:

brew install deno

Once installed, you can use the deno command. Running deno --help will show you the available options, such as starting a REPL or executing a script.

The Deno Commands

Deno provides several commands that you can run using the deno command. Some of the most commonly used commands are:

  • run: Run a program given a filename or URL to the module.
  • bundle: Bundle module and dependencies into a single file.
  • cache: Cache the dependencies.
  • fmt: Format source files.
  • install: Install a script as an executable.
  • types: Print runtime TypeScript declarations.
  • upgrade: Upgrade the Deno executable to the newest version.

You can use the --help option after any command to get more information on specific flags and options for that command. For example, deno run --help will provide detailed information about the run command.

Deno Code Examples

In addition to the examples provided in this guide, the Deno website has a collection of examples that you can explore at deno.land/std/examples/. These examples cover various use cases, such as file manipulation, networking, and more.

Your First Deno App

To run a Deno app, you can either write your own code or execute existing code from a URL. For example, you can run the following command to execute the Deno “welcome” program:

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

This program simply logs “Welcome to Deno 🦕” in the console. The Deno runtime will automatically download and compile the code the first time you run it. Subsequent runs will use the cached version.

Deno Sandboxing

Deno includes a sandbox that provides an extra layer of security. By default, Deno disallows applications from accessing the network or the file system without permission. You can grant specific permissions using command-line flags. For example, you can use the --allow-net flag to allow network access or the --allow-read flag to allow file system read access.

Formatting Code

Deno includes a built-in code formatter called deno fmt, which is similar to the gofmt command in Go. This command automatically formats your code to a consistent style. To use it, simply run deno fmt followed by the names of the files or directories you want to format.

Standard Library

Deno has an extensive standard library that covers various areas, including file system operations, network communication, cryptography, and more. You can find detailed information about the Deno standard library at doc.deno.land. This website provides comprehensive documentation and examples for all the modules in the standard library.

Conclusion

Deno is a promising alternative to Node.js, offering several improvements and a modern approach to server-side JavaScript development. While it may not replace Node.js entirely, it provides a compelling option for certain use cases, especially with its excellent TypeScript support and extensive standard library.

If you’re interested in learning more about Deno, consider exploring the official Deno website, the Deno API documentation, and the list of awesome Deno resources available on GitHub.