Deno is the new runtime for JavaScript and TypeScript, built on top of Google V8 and Rust. It is robust, secure, and flexible for dealing with web applications (both front and back end). It’s a project of Ryan Dahl, who set out to fix and improve things he didn’t like when he created Node.js.
Yes, Deno comes from the same creator of Node. It also embraces his experience over years of dealing with scenarios in which Node wasn’t quite everything he hoped it would be.
Of course, Node is great. And it’s going to thrive for years to come. So let’s take a closer look at Deno and explore the runtime and demonstrate its main features so you can form an opinion. Let’s go!
Main Deno Features
Deno comes with a bunch of fresh and nice features, such as:
Made with Rust, while Node was built with C++
Security – you can’t have direct access to things like files, networks, or environments. You must ask for permission first.
TypeScript out of the box. Sure, everybody’s using TS now.
There’s only one single file as the executable.
Lots of built-in dependencies for various actions like code formatting, dependency inspection, etc
And more
Setting Up Deno
Before going any further, it’s important to note that Deno has already gone through a couple of releases, so be sure to pay attention to the versions you’re working with since confusion here can lead to further issues.
In order to install Deno, simply follow the official instructions for your specific OS.
For Mac users, for example, run the following command:
brew install deno
At the end of the installation, run the following command to check if it was successful:
deno --version
You may see a similar output as follows:
➜ deno --version deno 1.7.0 (release, x86_64-apple-darwin) v8 8.9.255.3 typescript 4.1.3
That’s great because you get to see the V8 and TypeScript versions as well.
Exploring Deno Security
Let’s take a look at how Deno deals with securing things. As we’ve seen, Deno focuses on providing secure access to features like the file system, network, env variables, etc.
Take the following code snippet as an example:
Deno.writeFile('hi.txt', new TextEncoder().encode('Hi, Deno!'));
The code represents a good way to write some text to a file. The TextEncoder object helps with the proper encoding for the text passed to its encode() method. To run this code, save it to a file called index.js, for example, and run the following command:
deno run index.js
From which you’ll get the following error:
error: Uncaught (in promise) PermissionDenied: write access to "hi.txt", run again with the --allow-write flag throw new ErrorClass(res.err.message); ^ at processResponse (deno:core/core.js:212:11) at Object.jsonOpAsync (deno:core/core.js:229:12) at async open (deno:runtime/js/30_files.js:44:17) at async Object.writeFile (deno:runtime/js/40_write_file.js:54:18)
See? No direct access to any file system by default. Deno requires you to explicitly ask for this permission when executing the same file. It also prints proper instructions on how to achieve this.
Let’s run the following command:
deno run --allow-write index.js
This time the command finishes successfully. If you open the directory in which your index.js is located, you may find the hi.txt file there with the given content.
Deno Modules
With the ES6 advent, web browsers can load modules via URLs, as you’ve heard before. And so does Deno.
Node imports its modules via a package manager, like npm. Deno follows a different direction because this way, Deno packages can be anywhere on the internet without the need for a centralized registry manager.
Every time you run a Deno application, as you’ve done in the last example, Deno automatically downloads the required packages and keeps them within a local cache of its own. This way, it can manage them in such a way that no download is required for the same package and version anymore.
That’s very similar to how many programming languages operate today. If you’d like to refresh the dependencies for whatever reason, simply add the flag –reload to your command.
Importing a module, in turn, is very simple. Take the following as a reference:
import { Application } from "";
Deno’s built-in packages can be all found within the deno.land/x domain. This is Deno’s default package registry.
In the example above, Oak is the middleware framework for Deno’s http server inspired by Node’s Koa, including a router middleware.
By default, Deno modules are bundled with TypeScript, but it’s ok for you to import them into your JavaScript projects.
Take the following example on how to create a server and listen to requests at port 8000:
import { Application } from ""; const app = new Application(); app.use((ctx) => { ctx.response.body = "Hey, it's Deno here!"; }); await app.listen({ port: 8000 });
The Application object is Oak’s default entry point to create servers within Deno web apps. If you run it by simply typing the deno run index.js command, you may get the following error:
error: Uncaught PermissionDenied: network access to "0.0.0.0:8000", run again with the --allow-net flag throw new ErrorClass(res.err.message);
See? Again, we’re missing the proper permission for accessing a network resource. To fix it, run the following command:
deno run --allow-net index.js
This will lock the cursor waiting for interactions to come to your current server. If you enter the at your browser, you may see the welcome message show up.
ES and Browser Support
Deno was developed to work as your browsers do when it comes to URL importing. Because of this, and since we’re using ES modules to import things, there are no build tools like Webpack to help with browser compatibility.
Make sure you understand your project goals up front in order to check if Deno is the best fit. Deno has limitations because it embraces a whole new world that doesn’t include some old stuff.
You still can make use of Babel, for example, to transpile code to other versions of ES that your target browsers will accept. However, this can be tricky and lead to verbose and heavy final files. So, be careful with that option.
Conclusion
Deno is a promising technology to fix defective or lacking Node features.
It’s not widely used in production yet, but it’s definitely a tech we should keep our eyes on for the next couple of years.
Make sure to refer to its official docs for more on what you can do with Deno. Good luck!