Getting started with Deno runtime: an alternative to Node.js
In the past few days during the holidays, I have studied Deno . It is a replacement for Node.js. With it, Node.js may not be needed in the future.
This article is a preliminary introduction to Deno, trying to answer why Node.js cannot meet our needs, and what Deno can bring us?
The following content is mainly based on the latest speeches by Bert Belder and Ryan Dahl .
0,
Before entering the topic, let me talk about how to pronounce the word Deno.
I have heard two pronunciations, “Deno” and “Tino”. It seems that the pronunciation of “Tino” should be more appropriate, because Deno’s logo is a dinosaur. The abbreviation for dinosaur is dino.
1,
Deno was founded by Ryan Dahl in 2017.
Ryan Dahl is also the founder of Node.js. From 2007 to 2012, he later handed over Node.js to other developers, and instead of interfering with it, he turned to research artificial intelligence.
He has never liked the Python language very much. Over time, he wanted to develop an artificial intelligence development framework in the JavaScript language. When he turned around and picked up Node.js, he found that this project had deviated from his original intention and had some problems that could not be ignored.
2,
First of all, in the past five or six years, the JavaScript language has been completely reborn, and the ES6 standard has introduced a large number of new syntax features. Among them, there are two most influential syntax: Promise interface (and async function) and ES module.
Node.js’s support for these two new syntaxes is not ideal. Due to historical reasons, Node.js must support callback functions. As a result, there are two ways to write Promise and callback functions in asynchronous interfaces. At the same time, Node.js’s own module format CommonJS is incompatible with ES modules, which makes it impossible to fully support it. ES module.
Secondly, Node.js module management tool npm has more and more complex logic; the module installation directory npm_modules is extremely complex and difficult to manage. Node.js also has almost no security measures. As long as users download external modules, they have to let other people’s code run locally and perform various read and write operations.
Thirdly, the functions of Node.js are not complete, leading to endless external tools, making developers tired: webpack, babel, typescript, eslint, prettier…
3.
For these reasons, Ryan Dahl decided to abandon Node.js and write a replacement from scratch to completely solve these problems. The name deno is a recombination of letters from Node (Node = no + de), which means “demolition Node.js” (de = destroy, no = Node.js).
Like Node.js, Deno is also a server runtime, but supports multiple languages and can directly run JavaScript, TypeScript and WebAssembly programs.
It has a built-in V8 engine to interpret JavaScript. At the same time, a tsc engine is built in to interpret TypeScript. It is developed using the Rust language. Because Rust natively supports WebAssembly, it can also run WebAssembly directly. Its asynchronous operation does not use the libuv library, but uses the Tokio library of the Rust language to implement the event loop.
4.
You may ask, why use Rust instead of C++ (the development language of Node.js)?
The main reason is that Rust provides many ready-made modules, which can save a lot of development time for the Deno project.
5.
Deno itself is also a module of Rust. If you want to use the V8 engine in Rust, you can load Deno. It is equivalent to a V8 packaging layer, providing some low-level APIs for you to interact with the V8 engine.
6.
Deno has only one executable file, and all operations are done through this file. It supports cross-platform (Mac, Linux, Windows).
7.
Deno has security controls, and scripts do not have read and write permissions by default. If the script is not authorized, it reads and writes the file system or network, and an error will be reported.
You must use parameters to explicitly open permissions.
--allow-read
: Open the read permission, you can specify a readable directory, for example--allow-read=/temp
.--allow-write
: Open write permission.--allow-net=google.com
: Allow network communication, you can specify requestable domains, for example--allow-net=google.com
.--allow-env
: Allow reading environment variables.
8,
Deno supports Web API, try to be consistent with the browser.
It provides window as a global object, supports web standards such as fetch, webCrypto, worker, etc., and also supports event operation functions such as onload, onunload, and addEventListener.
In addition, all asynchronous operations of Deno always return Promise.
9,
Deno only supports ES modules, which are consistent with the browser’s module loading rules.
There is no npm, no npm_modules directory, no
require()
commands (that is, no CommonJS modules are supported), and no
package.json
files
are needed
.
All modules are loaded via URL, such as
import { bar } from "https://foo.com/bar.ts"
(absolute URL) or
import { bar } from './foo/bar.ts'
(relative URL).
Therefore, Deno does not need a centralized module storage system, and modules can be loaded from anywhere.
However, after Deno downloads the module, there will still be a general directory where the module is cached locally, so it can be used offline.
10.
Since Deno only supports loading modules from URLs, the Node.js module loading method will be invalid.
import React from "react"; import { Box, Grid } from "@material-ui/core"; import { initializeApp } from "firebase/app";
The above writing is illegal in Deno.
All modules of Deno must be loaded through the entry script, not through the module name, so they must have the script suffix name.
11.
Deno natively supports TypeScript language and can be run directly without explicit transcoding.
Internally, it will be judged according to the file suffix name. If it is a
.ts
suffix name, the TS compiler is called first to compile it into JavaScript; if it is a
.js
suffix name, it is directly passed to the V8 engine to run.
12.
Deno has built-in various functions that developers need, and no external tools are needed. There are special commands for packaging, formatting, testing, installation, document generation, linting, and compiling scripts into executable files.
Execute
deno -h
or
deno help
to display the subcommands supported by Deno.
deno bundle
: Packaging scripts and dependenciesdeno eval
: Execute codedeno fetch
: Grab the dependency to the localdeno fmt
: Formatting beautification of the codedeno help
: Equivalent to-h
parametersdeno info
: Display the local dependency cachedeno install
: Install the script as an executable filedeno repl
: Enter the REPL environmentdeno run
: Run the scriptdeno test
: Run the test
13.
You can refer to the homepage of the official website for the installation of Deno , but you can go directly to the release page of the GitHub repository and download the compiled executable file (above).
After downloading Deno, check the version.
$ deno --version deno 0.31.0 v8 8.1.108 typescript 3.7.2
Run the command line directly to
deno
enter the REPL environment.
$ deno > console.log(1,2,3) 1 2 3 undefined >
14.
Next, run a TypeScript remote script, which is an example given on the official website .
$ deno run \ https://deno.land/std/examples/curl.ts \ https://example.com
In the above example, Deno executes a remote script
curl.ts
and uses this script to fetch URLs
example.com
.
However, an error is reported after running, indicating that there is no network communication authority.
We give Deno network communication authority, and it can be executed smoothly.
$ deno run --allow-net \ https://deno.land/std/examples/curl.ts \ https://example.com
15.
Now, the latest version of Deno is 0.31. According to the plan, 1.0 should be released in the first half of this year.
Deno is still under intensive development and its functions are not stable. It is not recommended for use in a production environment. However, it is already a usable tool, you can try it out more and become familiar with its usage. I believe that many advantages in design will make it more advantageous than Node.js.
(over)