Deno is a new runtime environment for JavaScript and TypeScript. Deno founder Ryan Dahl, who was also the founding developer of Node.js, says Deno is intended to be a more modern version of Node.js.
When Node.js was first released, in 2009, JavaScript didn’t have the async-await mechanism, typed arrays, or the ECMAScript module system. At the time, Dahl recognized that an event loop would be an essential resource for server-side JavaScript and set to work on creating a runtime environment that included one.
By 2012, Dahl felt the primary goal of creating a user-friendly non-blocking framework had been achieved, with Node.js having support for multiple protocols, cross-platform support, a relatively stable API, and a growing ecosystem of external modules distributed through npm.
But over time it became apparent to Dahl that a number of design decisions, like the early removal of Promises, caused problems. Security, the GYP build system, and the lack of a core foreign function interface (FFI), index.js, and package.json + node_modules are persistent problems.
Rather than try to fix all the problems of Node.js and deal with the tons of legacy APIs, Dahl decided to build a new runtime system born of the wisdom and experience of the past decade. You can—and should—learn more about the origins of Deno in his 2019 JSConf EU talk, 10 Things I Regret About Node.js.
Deno characteristics
Deno is built with the Rust programming language and uses the V8 JavaScript engine, like Node.js.
Node.js and Deno have a lot in common, but there are a number of key differences:
- Deno supports TypeScript out of the box without requiring transcompilation into JavaScript.
- Deno’s main function is asynchronous, so you can use the
await
keyword in top-level code. - Deno is secure by default; it has no access to the file system, network, or environment variables as long as the user does not explicitly grant that access using command-line permission flags like
--allow-read
, --allow-write
, and --allow-net
. - The Deno project doesn’t include the package.json file and node_modules directory. All dependencies are included as native ECMAScript modules using the import statement, including a URI. (Node.js uses the CommonJS API for loading dependencies from the file system.)
- Downloaded dependencies are kept in the global cache directory and can be reused by other applications.
- Deno can run applications directly from a specified URL; there is no need to download the application to the disk.
- Deno ships with useful CLI tools like a dependency inspector (
deno info
) and code formatter (deno fmt
).
What you’ll learn in this post
In this post you’ll get hands-on experience programming in Deno. You’ll learn how to pass command-line arguments to Deno and use it to read files from the disk. You’ll also learn about the Deno security system and where to find third-party libraries for Deno programs. You’ll use the underscore.js library to compare objects for equality as a practical example of importing and using an external library.
Prerequisites
You’ll need the following tools to accomplish the task in this tutorial:
- Deno – Installation instructions are included in this tutorial.
- Git – Required to clone the companion repository or if you want to use it for source code control.
- Visual Studio Code – You can use another IDE or code editor, but these instructions are written with VS Code in mind.
- Deno extension for VS Code (denoland.vscode-deno) – If you’re using Visual Studio Code, this official extension provides Intellisense support and other important features.
Twilio account – It’s not required for this tutorial, but readers of this post can receive an additional $10 credit on their Twilio accounts when they sign up with this link.
To get the most out of this post you should also have a basic working knowledge of JavaScript. An understanding of the JavaScript callback loop, event queue, and Promises will also be helpful. See the other posts in this series listed in the Additional resources section if you need to get sharp on those concepts.
There is a companion repository containing the complete source code for the project available on GitHub.
Installing Deno
Deno is available through most of the popular package managers and the PowerShell Invoke-WebRequest utility: Pick the one that’s appropriate to your development environment and execute the command.
PowerShell (Windows, macOS, Linux):