Deno joins the battle
More than a decade after Node.js was released, a new runtime environment is here developed by Ryan Dhal, Node.js creator. A secure runtime to JavaScript/TypeScript built with Rust, Tokio and V8. Let’s make a brief walkthrough around its key features with a deno-example project.
Security
Deno works as a sandbox, blocking all direct accesses with the operating system (Disk, Network, Env variables, etc.). To execute your deno app you have to grant permission explicitly depending on how much access it’s going to have.
For example, if you want to execute our deno-example project you should run this:
deno --allow-net --allow-read server.ts
--allow-net
: allow network access--allow-read
: allow file system access
If you don’t specify the allow flags you will probably see an error like this on the console:
To display the list of available flags for permissions we can run the deno run -h
command.
Module system
CommonJS modules aren’t supported by Deno -it uses ESmodules as standard. Another breaking change is how dependencies are managed: package.json files are no longer necessary, and imports now are completely distributed instead of being in a centralized server like npm. The node_modules folder can be now disregarded. The new module system loads libraries directly from a URL, making explicit the extension of the file we want to use.
Remote code for external libraries is fetched and cached on first execution only. To update your dependencies you must use the run the --reload
flag.
TypeScript built-in
Nowadays, TypeScript became almost a standard for JavaScript development, it provides useful features like create own data types or the terrific intelliSense with some code editors.
TypeScript is the default language for Deno projects, and the tsc
compiler forms part of his core. That saves us time since it’s supported without any additional configuration.
However, we can use just JavaScript files if we don’t want to use TypeScript.
Tooling built-in
Deno vision embraces the idea of having a complete toolset available for common tasks to reduce time-wasting configuration, one good example of this might be:
Testing
Testing frameworks are in a good place these days, there are plenty of options out there like Jest, Mocha, and so on. Unit tests work like a quality assurance that ensures that everything should work as expected, deno includes a test runner between his built-in tools. Also, a couple of third-parties libraries are coming to help to create mocks, spies, and stubs.
For example, we are going to run the following unit test that simulates a Post creation.
We have to rundeno test [files]
and indicate the files we want to test. In our case, the command looks like this demo test tests/*.ts
Notice that we got an error because Deno prevents the code execution whether we don’t use the required flags, we can give another try using the deno test --allow-read tests/*.ts
. This time it isn’t necessary to use the --allow-net
flag because the fetch
method was mocked in our unit test.
Formatting
The code format is fundamental these days between developer teams, as it helps to keep consistency around how the code looks like, tools such as ESlint
or Prettier
are well known to facilitate this task. Deno incorporates its own auto-format source code within its utilities.
For example, a code like this one doesn’t follow any formatting style, it lacks indentation and it’s missing semicolons in some places.
After run deno fmt
command the code is formatted
Libraries
Deno provides two essential types of libraries:
- Standard: They don’t have any external dependencies, they’re maintained by the core Deno team directly. It compounds from operating system manage like fs, http, and path
to utility libraries like testing and log
, among others.
- Third-Party: They are created by the community, decentralized and can be hosted on the public deno directory at deno.land
Conclusion
Is Deno here to replace Node.js for good? It’s too soon to tell. Node.js is used by millions of developers not only for backend development; it’s present in most of the tools that we use like CLIs or the npm itself and, also, it has a mature and active community that grows every day. Deno is here to be a good alternative bringing improvements learned over the last decade using JavaScript on the server. And it comes with a glimpse of how backend may look like for Javascript developers.