IO /what-is-den… blog.bitsrc. IO /what-is-den…

Will Deno replace NodeJS?

It may be too early to tell, but a few examples are proving the case

Let’s get started

In 2018, Ryan discussed 10 design mistakes in Node.js. And finally he announced Deno, which was a small project at the time and was also called improved and more secure Node.js 2.0 (introductory video at the time)

Two years later, Deno 1.0 was officially released on May 13. For the back end, it’s the new javascript runtime, not c++, but Rust, and based on the Tokio platform (which supports asynchronous js operations called time loops), but still running on Google’s V8 engine.

Is this it?

We are not saying that this new JS runtime is fully compatible with current Node.js. Instead,Ryan wants to put some of the flaws and ill-considered aspects of building Node into Deno.

Integrated security

Node.js allows you to access any file by default, which means you can read and write directly to the file system, make requests directly out of it, get environment variables directly, and more. While there are benefits to having these permissions as a developer, there are also security risks when you don’t care much about the code you’re writing.

Instead, Deno uses command-line arguments to allow or static access to different security features. When you want to access the /etc folder with script, you can:

deno --allow-read=/etc myscript.ts
Copy the code

This allows your code to read folders, and you can get security exceptions. This is very similar to other platforms getting security. If you are an Android user, many apps installed on your phone need to be asked before accessing different systems (e.g. contacts, calls, folders), etc. This is the same level. You need to provide permission on your code while adding these parameters to execute your script using the command line.

A more complete standard library

JavaScript has provided a standard library since the first Node release, but it still has a long way to go compared to other languages. Deno is also working on these improvements and claims to have a very good standard library that allows developers to use official tools to perform basic functions and perform responsible tasks with only a few third-party libraries.

Essentially out of the box, Deno’s utility color is used directly on command line text, handles data structures (e.g. binary, CSV, YAML, etc.), generates UUIDs, writes websockets directly, and more basic modules are available, such as file access, Date helper functions, HTTP request-related functions have been added

Built-in TypeScript

Yes, if you’re a fan of TypeScript,Deno already does it for you, with no extra configuration and internal translation to JavaScript

While Deno has done a lot for you, you can rewrite the configuration project to use a custom tsconfig.json file:

deno run -c tsconfig.json [your-script.ts]
Copy the code

The default configuration uses strict mode, and non-standard code immediately throws warnings.

No more NPM or node_modules folders

Isn’t that too bloated? Is this a wrong way to control dependent distribution? This is the most controversial aspect of Node, and Deno decided to get rid of it.

So how does Deno handle dependencies? You can load templates from anywhere. In other words, you can simply do this:

import * as log from "https://deno.land/std/log/mod.ts"
Copy the code

No longer need a centralized repository, you have to be careful when importing third-party source code, especially if you don’t fully master it, which leaves your code exposed.

In fact, package.json is gone, and dependency management is now very simple, with a list of modules and URLs in a file called deps.ts. But what about version control? Just specify the version of the package on the URL, and it doesn’t look very elegant, but it works.

Demos.ts looks something like this

export { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
export { green, bold } from "https://deno.land/[email protected]/fmt/colors.ts";
Copy the code

You can export different versions of the package by simply changing the URL.

By the way, the first imported code is cached until you use the –reload flag when executing your script

Anything else?

Deno also has other features, including more out-of-the-box tools like Test Runner, Debugger, File Watcher, and more. But by the way, some of them just provide APIs, and you’ll have to write your own tools to use them.

For example: The File Watcher API is provided to you via deno.Watchfs. If you want to find a solution similar to Nodemon’s, you need to implement it yourself. Here is an implementation

function startProcess(args: string[] = []) :Deno.Process {
	return Deno.run({ cmd: ['deno'. args]}) }const throttle = 500;
let app: Deno.Process = startProcess(Deno.args);
let appInitTime: number = Date.now();
let timeout: number|null = null;

funtion runApp(){
	appInitTime = Date.now();
    app && app.close();
    app = startProcess(Deno.args);
}

runApp();

for await (const event of Deno.watchFs('. ')) {
	if(event.kind ! = ="access") {
    	if (timeout) clearTimeout(timeout);
        timeout = setTimeout(runApp, throttle)
    }
}
Copy the code

Will it replace Node.js sooner or later?

No, back in The 0.10 version of Node, we were using Node.js and using it directly in production! May now be a little afraid, then do so may be no similar things. It’s not PHP,Python, or even Ruby that can write back-end Javascript with asynchronous I/O operations. Over the years, Node has grown to meet the needs of the industry. It’s not perfect, but there are no perfect choices in programming languages.

Deno isn’t unique, it’s probably just going to be a hot topic for the next couple of years, and it hasn’t been used in products yet. It is untested in dealing with some border issues. When it does, it may become a toy for some early adopters. Over the years, you’ll hear about companies experimenting with Deno and sharing their experiences on how to solve these problems, and the community behind it will iterate on new problems. And then it can replace Node? Wait and see.

Afterword.

When I finished translating this article, nuggets inadvertently sent me an article to read if you are interested. Juejin. Cn/post / 689933…