If you are a frontend developer, you would have used the Node Package Manager (NPM) in your applications. It is the default package manager for the JavaScript runtime environment, NodeJs. NPM has been among us for so long, and there is a massive community around it that uses it daily.
如果您是前端开发人员,则应在应用程序中使用节点包管理器(NPM)。 它是JavaScript运行时环境NodeJs的默认包管理器。 NPM已经存在我们很久了,周围有庞大的社区每天都在使用它。
However, did you ever think that NPM and it’s functionality will be replaced soon? Will there be an era where a frontend developer does not know of NPM and use a different method for managing packages and dependencies in an application?
但是,您是否曾经想到过NPM及其功能会很快被替换? 会有一个时代,前端开发人员不了解NPM,而是使用其他方法来管理应用程序中的程序包和依赖项吗?
These questions did not surface until the introduction of Deno.
直到Deno的出现,这些问题才浮出水面。
The goal of this article is to explore the following.
本文的目的是探索以下内容。
- What is Deno? 什么是德诺?
- A comparison between Node.js and Deno Node.js和Deno之间的比较
- Will Deno replace Node.js and kill NPM?迪诺会取代Node.js并杀死NPM吗?
1.什么是迪诺? (1. What is Deno?)
Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.
Deno是使用V8并内置于RustJavaScript和TypeScript的简单,现代且安全的运行时。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/be8e99a98a451eae3c50ce5fe6bc9ebf.png)
Ryan Dahl, the original creator of Node.js, introduced Deno at the JSConf EU 2018. He introduced Deno as a better half of Node.js. Deno can act as both the runtime and package manager within a single executable rather than requiring a separate package manager like in Node.js. It is built on top of,
Node.js的原始创建者Ryan Dahl在JSConf EU 2018上介绍了Deno。他介绍了Deno作为Node.js的更好一部分。 Deno可以在单个可执行文件中同时充当运行时和程序包管理器,而无需像Node.js中那样需要单独的程序包管理器。 它建立在
- V8 (similar to Node.js runtime)V8(类似于Node.js运行时)
- Rust (instead of C++) Rust(而不是C ++)
- TypeScript (instead of VanillaJS) TypeScript(代替VanillaJS)
- Tokio (instead of the libuv event loop in Node.js) Tokio(而不是Node.js中的libuv事件循环)
So why is Deno considered better than Node.js and NPM? There are a few reasons behind this. Let’s look at the main improvements to Node.js, which was introduced with Deno.
那么为什么Deno被认为比Node.js和NPM更好呢? 这背后有几个原因。 让我们看一下Deno引入的对Node.js的主要改进。
Tip: Share your reusable components between projects using Bit (Github). Bit makes it simple to share, document, and organize independent components from any project.
提示:使用Bit ( Github )在项目之间共享可重用组件。 Bit使共享,记录和组织来自任何项目的独立组件变得简单。
Use it to maximize code reuse, collaborate on independent components, and build apps that scale.
使用它可以最大程度地重复使用代码,在独立组件上进行协作以及构建可扩展的应用程序。
Bit supports Node, TypeScript, React, Vue, Angular, and more.
Bit支持Node,TypeScript,React,Vue,Angular等。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/9c41eff6a87f8e6676a90f4e04d3eb92.png)
2. Node.js与Deno的比较(2. A comparison between Node.js and Deno)
包管理器的句号(A full stop to Package Managers)
Deno does not have a centralized package manager nor a package.json. This means that Deno does not support NPM modules directly.
Deno没有集中式的包管理器,也没有package.json。 这意味着Deno不直接支持NPM模块。
Then how does Deno install dependencies?
那么,Deno如何安装依赖项?
Instead of downloading dependencies via a package manager and maintaining a large folder like node_modules
, Deno uses modules referenced as URLs or file paths. Have a look at the examples below.
node_modules
不用通过程序包管理器下载依赖项并维护像node_modules
这样的大文件夹, node_modules
使用引用为URL或文件路径的模块。 看下面的例子。
import * as log from "https://deno.land/std@0.68.0/log/mod.ts";
import { serve } from “https://deno.land/std@0.50.0/http/server.ts";
Once you start the application, Deno will download all the modules and cache them. However, if you need to download the modules again, you need to request for it explicitly using the --reload
flag.
启动应用程序后,Deno将下载所有模块并缓存它们。 但是,如果需要再次下载模块,则需要使用--reload
标志来明确请求。
Even though Deno does not support NPM modules directly, it provides a Node Compatibility Library, which allows you to use some NPM modules where necessary.
尽管Deno不直接支持NPM模块,但它提供了Node Compatibility Library(节点兼容性库),使您可以在必要时使用某些NPM模块。
Below is an example of how this can be used.
下面是如何使用它的示例。
import { createRequire } from "https://deno.land/std/node/module.ts";const require = createRequire(import.meta.url);
const esprima = require("esprima");const program = 'const answer = 42';
console.log(esprima.tokenize(program));
To run this, you will have to use the flag --allow-read
. The below is restricted to only read the folder node_modules
.
要运行此程序,必须使用--allow-read
标志。 以下内容仅限于读取文件夹node_modules
。
deno run --allow-read=node_modules esprima.js
This method provides some level of compatibility with Node.js and NPM.
此方法提供了与Node.js和NPM的某种程度的兼容性。
Deno更安全。 (Deno is more secure.)
Secure by default. No file, network, or environment access, unless explicitly enabled.
默认为安全。 除非明确启用,否则没有文件,网络或环境访问权限。
If you run an application using the Node.js runtime, your application would have direct access to your network, computer, system calls, file system, and environment variables. This could be a very high-security threat. However, with Deno, all of these direct accessibilities are removed. Hence, Deno is introduced as secure by default. This level of security is achieved by making Deno run on a sandbox. If the application requires certain access levels, you need to specify them explicitly and allow access using flags.
如果使用Node.js运行时运行应用程序,则您的应用程序将可以直接访问网络,计算机,系统调用,文件系统和环境变量。 这可能是一个非常高的安全威胁。 但是,使用Deno,所有这些直接可访问性都将被删除。 因此,默认情况下,Deno被引入为安全的。 通过使Deno在沙盒上运行,可以达到这种安全级别。 如果应用程序需要某些访问级别,则需要明确指定它们,并允许使用标志进行访问。
Let’s have a look at an example.
让我们看一个例子。
If you need to allow Deno to access the network, the following flag needs to be added to the command.
如果需要允许Deno访问网络,则需要在命令中添加以下标志。
deno run --allow-net MyDenoScript.ts
Likewise, there are many flags available to allow specific access levels to your application when using Deno. The security options that could be allowed are as follows.
同样,使用Deno时,有许多标志可用于允许对应用程序进行特定的访问级别。 可能允许的安全性选项如下。
--allow-all # Allow all permissions
--allow-env # Allow environment access
--allow-hrtime # Allow high resolution time measurement
--allow-net=<allow-net> # Allow network access
--allow-plugin # Allow loading plugins
--allow-read=<allow-read> # Allow file system read access
--allow-run # Allow running subprocesses
--allow-write=<allow-write> # Allow file system write access
Deno支持TypeScript (Deno supports TypeScript)
TypeScript is extremely popular among the developer community nowadays. It is a superset of JavaScript. Deno directly supports TypeScript and does not require you to set up your environment to use TypeScript manually. This is a massive advantage as it could improve your development speed.
如今,TypeScript在开发人员社区中非常受欢迎。 它是JavaScript的超集。 Deno直接支持TypeScript,不需要您将环境设置为手动使用TypeScript。 这是一个巨大的优势,因为它可以提高您的开发速度。
Apart from the following, Deno also has the following features which differ from Node.js.
除以下内容外,Deno还具有以下与Node.js不同的功能。
Set of reviewed standard modules. (These are guaranteed to work with a specific Deno version).
一套复习的标准模块。 (保证可以与特定的Deno版本一起使用)。
- Deno returns a promise for asynchronous actions. Deno返回对异步操作的承诺。
- Always dies on uncaught errors. 始终死于未捕获的错误。
- Deno is shipped as a single executable. Deno作为单个可执行文件提供。
Due to these advantages and features, Deno became the talk of the town after it’s release.
由于这些优点和功能,Deno发布后成为该镇的话题。
3.迪诺会取代Node.js并杀死NPM吗? (3. Will Deno replace Node.js and kill NPM?)
We have been exploring the different features of Deno and how it differed from the Node.js runtime all this time. Now comes the real question. Will Deno kill NPM? Will we abandon the concept of package managers in the future?
我们一直在探索Deno的不同功能,以及它与Node.js运行时的不同之处。 现在是真正的问题。 迪诺会杀死NPM吗? 将来我们会放弃打包管理器的概念吗?
I don’t think so!
我不这么认为!
In my opinion, the introduction of Deno would not kill Node.js or its package manager. The reason behind this is, Node.js and NPM have been used in many many applications for over a decade. The Node.js ecosystem is too large to be replaced. Unless Deno is 100% compatible with Node.js, we won’t be able to migrate every application to use Deno.
我认为,引入Deno不会杀死Node.js或其包管理器。 其背后的原因是,十多年来,Node.js和NPM已在许多应用程序中使用。 Node.js生态系统太大,无法替换。 除非Deno与Node.js 100%兼容,否则我们将无法迁移每个应用程序以使用Deno。
However, Deno can be used in new applications. Due to its secure nature and other advantages, it’s likely to be popular among the community for new applications. Some of the old applications may even start using Deno at some point. But, in my opinion, Deno would not replace Node.js and kill NPM completely. Due to its heavy usage and massive ecosystem, we can expect Node.js and NPM to stick around for a long time in the future too.
但是,Deno可以用于新应用程序中。 由于其安全性和其他优势,它很可能在社区中流行用于新应用程序。 某些旧应用程序甚至可能在某个时候开始使用Deno。 但是,我认为Deno不会取代Node.js并彻底杀死NPM。 由于其大量使用和庞大的生态系统,我们可以预期Node.js和NPM在未来还会长期存在。
If you are a tech enthusiast, it is always important to explore new technology and determine whether it is usable in your next big project. Therefore, even if Deno may not replace Node.js, there is no harm in learning it and using it when necessary and applicable.
如果您是技术爱好者,那么探索新技术并确定其在下一个大型项目中是否可用始终很重要。 因此,即使Deno不能取代Node.js,学习它并在必要和适用时使用它也没有害处。