Node.js was created by Ryan Dahl in 2009, that handles web server requests asynchronously. For example, if there is a request to open a file, Node.js will pass the request to the file system and immediately get on with the next request. Once the file system has processed the previous request, the server simply returns the response (in this case file content) to the client. Node.js is thus memory efficient and can handle requests faster. Node.js can collect form data, generate dynamic page content, perform all the file operations like open, close, read, write, delete on the server, and add, delete or update data present in the database.
Deno, developed by the same person – Ryan Dahl – is said to be a better version of Node.js that takes care of Node.js’s shortcomings. It provides a more secure JavaScript runtime environment. The thought of replacing Node.js with Deno came because of the changes in JavaScript, notably the TypeScript compiler. Deno fully supports the ES Modules as opposed to Node.js that supported the CommonJS.
If you haven’t already noticed, Deno is the anagram for Node!
Deno is a secure TypeScript runtime. It is like a shell for the Google JavaScript runtime engine V8.
Deno is an open-source project that supports TypeScript and JavaScript. It is built on Rust. Deno offers better security and has no package manager, unlike Node.js (npm).
Some unique features of Deno are:
Strong security and permissions mechanisms – the runtime doesn’t have access to environment variables, file system, other scripts or network.
Reliable and provides high performance
Easier for developers as it provides many built-in features including testing, debugging, formatting tools
Can automatically compile TypeScript
Cross-browser support
Deno doesn’t support require(); modules are imported using URLs
To appreciate Deno and its features, we will quickly see how easy it is to install and use Deno and then we will move on to understanding the differences between Deno and Node.js.
Installation
Installation is simple – just download the binary executable and install it using the command-line interface (Windows). There are no external dependencies.
Check the deno website for installation.
Upon installation, you have to include DENO_DIR (path) to your environment variables.
A simple program
Deno files are saved with an extension “.ts” (typescript). Let us create the most straightforward program that echoes a welcome message to the user.
Let’s have a home.ts file which is the main application script.
To execute this script (or .ts), we can type the entire path of the file on the command line interface with the following command:
deno run http://demofiles/example/home.ts
In the file, simply type, console.log(‘Welcome aboard, lots of learning here’)
If you run this using the above command, the message will be printed in the CLI window.
All typescript files are stored as web pages, and if you open home.ts, it will open on a browser.
As we have understood, both Node.js and Deno serve the same purpose. Both are built on top of V8, Google’s runtime engine, and perform well for developing server-side with JavaScript. As of today, Node.js is a technology that has established itself well over the years, and Deno is relatively new, and some of its features are still under development. Hence, we cannot think of Deno as an immediate replacement for Node.js; however, the long-term goal could be to have an alternative. Let us understand what Deno offers better than Node.js to know more:
Node.js | Deno |
---|---|
written in C++ | written in TypeScript and Rust |
has a package manager named npm | no bulky package manager, ES modules are imported using URLs, making it more flexible and achieving decentralization |
Programs can access everything that the user can access | enhanced security using permissions. The program can only access parts set by the user using flags like --allow-write, --allow-net, --allow-env, --allow-run |
uses CommonJS syntax to import packages | uses the ESModules – import { <modulename> } from "<actual-url>" ; |
Standard library and API are based on callbacks | All the API and standard library of Deno use ECMAScript features that make use of Promise and ES |
‘await’ is always wrapped inside the ‘async’ function | supports top-level ‘await’ so that it can be used in the main script |
all the packages are centralized | packages are imported from URL and once done, they are cached and compiled locally, and updated only when there is explicit refresh |
all the npm packages contain package.json that stores the metadata about the project like project dependencies | doesn’t use package.json for module resolution |
We have compared the most important features of Node.js and Deno and understood that Deno is a significant improvement over Node.js and focuses on high security and performance. Further, from the developer’s point of view, Deno is easy to code and execute and doesn’t need an external compiler because developers can write and build scripts in TypeScript. While Node has a great community and several projects depend on Node.js, Deno was created as Ryan Dahl felt that he could have done away with several features of Node.js like security, centralization etc. in his famous 2018 talk “10 Things I Regret About Node.js“ while announcing about Deno. In a few years, Deno could be a great contender for other JavaScript runtime environments, once all of its features are put live.