Say yes to Deno?
Tags: typescript, javascript, codequality, webdev
Published: 2021-02-26 15:04:05

####Let’s talk about Deno and why it may be the better alternative to Node.js. 

Before we start, what is Deno? 
>Deno is a command-line runtime for executing JavaScript and TypeScript. It’s a framework around V8, uses Rust, and has the TypeScript compiler built into it.

#####Cool Deno features we will mention: 
* Secure execution by default
* Single executable
* Import from URLs
* Simple and browser-compatible module system
* TypeScript out of the box
* Full development environment
* Full toolset

####So why Deno? 
If Node.js is working, why rewrite it? Well, first of all, JavaScript has changed considerably since Node.js was designed in 2009. For example, async/await and promises are now standard. JavaScript now also has a new module system (ES Modules), typed arrays (no concept of using binary data inside of JS when Node was originally designed), and TypeScript, which has been a very nice supplement to JavaScript.

#### Node.js has room for improvement
Secondly, Node.js has some problems, a few of which Ryan Dahl mentioned in a talk introducing Deno. With the package management system centralized in one location, the module system is poorly designed. Another issue is with regards to the topic of security. Although V8 itself is a secure sandbox, Node has compromised this sandbox in order to do things such as opening sockets and reading files, etc. When you’re running a Node program, you lose security.

####Secure execution by default
Deno on the other hand is a secure execution by default, meaning when you run a program in Deno, you don’t need to trust it because, by default, you have no access to the system and can’t open files or sockets. Deno enables people to run untrusted third-party code safely.
For your app to be allowed to access the network, it must be given authorization with the command:
`deno run --allow-net app.ts`  

To interact with the system, Deno allows the user to opt in to various permissions such as reading from files systems with the command
`--allow-read=/temp`.

####Single executable
This is another perk of Deno and it has been told that it will always remain this way. Deno ships on Mac, Linux, and windows. It is aimed to be kept independent from system libraries.

####Import from URLs
Now for the most obvious change.
`import {serve} from “https://deno.land/std@0.50.0/http/server.ts”`
In Deno, you can import from URLs, the idea being to replace the entire modules system. This enables more distribution, as code can now be served up from any URL. The module resolution scheme in Node is a bit more complicated, as there are node_modules and index.js to worry about, which is troublesome for a lot of systems including TypeScript.


Deno’s approach is more simple, using only relative or absolute URLs. Deno does not rely on NPM at all — instead, you can import libraries via the URL. In Deno, modules and files are regarded as the same concept, which simplifies the mental model for users. 

####Simple and browser-compatible module system
Deno has a module system that is not dependent on a centralized server. The runtime fetches, caches, and compiles code transparently. It can work offline using a download cache. This provides the possibility to work and fetch resources even when there is no access to the internet, simulating the same process as when you npm install something. On the first run, you can `deno fetch` to download and cache these dependencies. 

####TypeScript out of the box
Most importantly, Deno supports TypeScript out of the box, meaning that TS is compiled into Deno. The feature V8 Snapshots is used to start up the TS compiler quickly, with no need to recompile each time. It can also deal with JavaScript, without any problem, out of the box.

####Full development environment
Since all dependencies are expressed as code, there is no need for package.json. `deno info {url}` gives you a dependency tree of all modules that were loaded in there. `deno info` also works on ES modules, even if there are random ES modules on the web nothing to do with Deno. 

####Full toolset
Deno’s vision is to provide a full toolset in a single binary. 
With commands such as `deno install`, `deno info`, `deno bundle`, and `deno fmt`, Deno acts like a multi-functional tool to run programs without having anything else installed on your machine.

####Final thoughts
So while Deno and Node.js both share the same origin, there are some strong arguments for why Deno may indeed be the easier and more efficient choice. But this, by far, does not mean that Node will be replaced, at the end of the day, it is up to user preference. 

Let us know what you think about Deno and Node.js! Which one do you prefer?

One more thing to note is that Codesphere fully supports Node.js and TypeScript and we are happy to introduce Deno support out of the box as well if there is a demand for it within our community. :)