赞
踩
deno知乎
Tip: this article is pretty long, you can download it as PDF / ePub / Mobi to read it later
提示:本文相当长,您可以将其下载为PDF / ePub / Mobi以便稍后阅读
I explore new projects every week, and it’s rare that one hits my attention so much as Deno did.
我每周都会探索新项目,很少有人会像Deno一样受到我的关注。
If you are familiar with Node.js, the popular server-side JavaScript ecosystem, then Deno is just like Node. Except deeply improved in many ways.
如果您熟悉流行的服务器端JavaScript生态系统Node.js,那么Deno就像Node。 除了在很多方面都得到了深深的改善。
Let’s start from a quick list of the features I like the most about Deno:
让我们从我最喜欢的Deno快速功能列表开始:
It embraces ES modules
它包含ES模块
It has a first-class await
它有一流的await
It aims to be browser-compatible as much as it can, for example by providing a built-in fetch
and the global window
object
它旨在尽可能地与浏览器兼容,例如,通过提供内置的fetch
和全局window
对象
We’ll explore all of those features in this guide.
我们将在本指南中探索所有这些功能。
After you use Deno and learn to appreciate its features, Node.js will look like something old.
在使用Deno并了解了其功能之后,Node.js看起来会很旧 。
Especially because the Node.js API is callback based, as it was written way before promises and async/await. There’s no change in place for that in Node, as such a change would be monumental, so we’re stuck to callbacks or to promisifying API calls.
特别是因为Node.js API是基于回调的,因此它是在promise和async / await之前编写的。 Node中没有任何更改,因为这样的更改将是巨大的,因此我们只能使用回调或大量使用API调用。
Node.js is awesome and will continue to be the de facto standard in the JavaScript world. But I think we’ll gradually see Deno more adopted because of its first-class TypeScript support and modern standard library.
Node.js 非常棒 ,并将继续成为JavaScript世界中的事实上的标准。 但是我认为我们将逐渐看到Deno,因为它具有一流的TypeScript支持和现代标准库。
Deno can afford to have everything written with modern technologies, since there’s no backward compatibility to maintain. Of course there’s no guarantee that in a decade the same will happen to Deno and a new technology will emerge, but this is the reality at the moment.
由于没有向后兼容性,因此Deno可以用现代技术编写所有内容。 当然,我们无法保证十年之内Deno也会发生同样的事情,并且会出现一项新技术,但这是目前的现实。
Deno was announced almost 2 years ago by the Node.js original creator Ryan Dahl at JSConf EU. Watch the YouTube video of the talk, it’s very interesting and it’s a mandatory watch if you are involved in Node.js and JavaScript in general.
大约2年前,Node.js的原始创建者Ryan Dahl在JSConf EU上宣布了Deno。 观看YouTube演讲视频 ,这非常有趣,如果您通常参与Node.js和JavaScript,那么这是必看的。
Every project manager must take decisions. Ryan regretted some early decisions in Node. Also, technology evolves, and today JavaScript is a totally different language than what it was back in 2009 when Node started. Think about the modern ES6/2016/2017 features, and so on.
每个项目经理都必须做出决定。 Ryan对Node中的一些早期决定感到遗憾。 此外,技术也在不断发展,如今JavaScript与2009年Node创立时的语言已经完全不同。 考虑一下现代的ES6 / 2016/2017功能,等等。
So he started a new project to create some sort of second wave of JavaScript-powered server side apps.
因此,他开始了一个新项目,以创建第二波基于JavaScript的服务器端应用程序。
The reason I am writing this guide now and not back then is because technologies need a lot of time to mature. And we have finally reached Deno 1.0 (1.0 should be released on May 13, 2020), the first release of Deno officially declared stable.
我现在而不是那时写本指南的原因是,技术需要大量时间才能成熟。 我们终于达到了Deno 1.0 (1.0应该在2020年5月13日发布),这是Deno的第一个正式宣布稳定的版本。
That’s might seem just a number, but 1.0 means there will not be major breaking changes until Deno 2.0, which is a big deal when you dive into a new technology - you don’t want to learn something and then have it change too fast.
这似乎只是个数字,但1.0意味着直到Deno 2.0才有重大突破,当您采用一种新技术时,这很重要-您不想学习任何东西,然后让它改变得太快。
That’s a big question.
这是一个大问题。
Learning something new such as Deno is a big effort. My suggestion is that if you are starting out now with server-side JS and you don’t know Node yet, and never wrote any TypeScript, I’d start with Node.
学习诸如Deno之类的新东西是一项巨大的努力。 我的建议是,如果您现在开始使用服务器端JS,并且您还不了解Node,并且从未编写任何TypeScript,那么我将从Node开始。
No one was ever fired for choosing Node.js (paraphrasing a common quote).
没有人因为选择Node.js而被解雇(用一个共同的说法解释)。
But if you love TypeScript, don’t depend on a gazillion npm packages in your projects and you want to use await
anywhere, hey Deno might be what you’re looking for.
但是,如果您喜欢TypeScript,则不要依赖项目中庞大的npm软件包,而想要await
任何地方使用await
,那么Deno可能就是您想要的。
No. Node.js is a giant, well established, incredibly well supported technology that is going to stay for decades.
不会。Node.js是一项庞大的,建立良好的,获得了难以置信的良好支持的技术,它将持续数十年。
Deno is written in Rust and TypeScript, two of the languages that today are really growing fast.
Deno用Rust和TypeScript编写,这两种语言今天正在Swift发展。
In particular being written in TypeScript means we get a lot of the benefits of TypeScript even if we might choose to write our code in plain JavaScript.
特别是用TypeScript编写意味着即使我们可能选择使用纯JavaScript编写代码,我们也可以获得TypeScript的很多好处。
And running TypeScript code with Deno does not require a compilation step - Deno does that automatically for you.
使用Deno运行TypeScript代码不需要编译步骤-Deno会自动为您完成此步骤。
You are not forced to write in TypeScript, but the fact the core of Deno is written in TypeScript is huge.
您没有被迫使用TypeScript编写代码,但是Deno的核心是用TypeScript编写的事实是巨大的。
First, an increasingly big percentage of JavaScript programmers love TypeScript.
首先,越来越多JavaScript程序员喜欢TypeScript。
Second, the tools you use can infer many information about software written in TypeScript, like Deno.
其次,您使用的工具可以推断出许多有关用TypeScript编写的软件的信息,例如Deno。
This means that while we code in VS Code for example, which obviously has a tight integration with TypeScript since both are developed at MicroSoft, we can get benefits like type checking as we write our code, and advanced IntelliSense features. In other words the editor can help us in a deeply useful way.
这就是说,例如,尽管我们使用VS Code进行编码,而这显然是与TypeScript紧密集成的,因为二者都是在MicroSoft上开发的,但我们在编写代码时可以获得类型检查和高级IntelliSense功能等好处。 换句话说,编辑器可以以非常有用的方式帮助我们。
Since Deno is basically a Node.js replacement, it’s useful to compare the two directly.
由于Deno基本上是Node.js的替代品,因此直接比较两者非常有用。
Similarities:
相似之处:
Both are developed upon the V8 Chromium Engine
两者都是基于V8Chrome引擎开发的
Differences:
差异:
Node has an official package manager called npm
. Deno does not, and instead lets you import any ES Module from URLs.
Node有一个名为npm
的官方软件包管理器。 Deno不会,而是让您从URL导入任何ES模块。
Deno has for a long time envisioned the possibility of compiling a program into an executable that you can run without external dependencies, like Go, but it’s still not a thing yet. That’d be a game changer.
Deno长期以来一直在考虑将程序编译成可执行文件的可能性,您可以在无需外部依赖项的情况下运行该可执行文件,例如Go,但这还不是一件容易的事 。 那将改变游戏规则。
Having no package manager and having to rely on URLs to host and import packages has pros and cons. I really like the pros: it’s very flexible, we can create packages without publishing them on a repository like npm.
没有程序包管理器并且必须依靠URL来承载和导入程序包是有利有弊。 我真的很喜欢专家:它非常灵活,我们可以创建软件包,而无需在npm这样的存储库中发布它们。
I think that some sort of package manager will emerge, but nothing official is out yet.
我认为某种软件包管理器将会出现,但是还没有官方的消息。
The Deno website provides code hosting (and thus distribution through URLs) to 3rd party packages: https://deno.land/x/
Deno网站为第三方软件包提供代码托管(并因此通过URL分发): https : //deno.land/x/
Enough talk! Let’s install Deno.
聊够了! 让我们安装Deno。
The easiest way is to use Homebrew:
最简单的方法是使用Homebrew :
brew install deno
Once this is done, you will have access to the deno
command. Here’s the help that you can get using deno --help
:
完成此操作后,您将可以访问deno
命令。 这是您可以使用deno --help
获得的帮助:
- flavio@mbp~> deno --help
- deno 0.42.0
- A secure JavaScript and TypeScript runtime
-
- Docs: https://deno.land/std/manual.md
- Modules: https://deno.land/std/ https://deno.land/x/
- Bugs: https://github.com/denoland/deno/issues
-
- To start the REPL, supply no arguments:
- deno
-
- To execute a script:
- deno run https://deno.land/std/examples/welcome.ts
- deno https://deno.land/std/examples/welcome.ts
-
- To evaluate code in the shell:
- deno eval "console.log(30933 + 404)"
-
- Run 'deno help run' for 'run'-specific flags.
-
- USAGE:
- deno [OPTIONS] [SUBCOMMAND]
-
- OPTIONS:
- -h, --help
- Prints help information
-
- -L, --log-level <log-level>
- Set log level [possible values: debug, info]
-
- -q, --quiet
- Suppress diagnostic output
- By default, subcommands print human-readable diagnostic messages to stderr.
- If the flag is set, restrict these messages to errors.
- -V, --version
- Prints version information
-
-
- SUBCOMMANDS:
- bundle Bundle module and dependencies into single file
- cache Cache the dependencies
- completions Generate shell completions
- doc Show documentation for a module
- eval Eval script
- fmt Format source files
- help Prints this message or the help of the given subcommand(s)
- info Show info about cache or info related to source file
- install Install script as an executable
- repl Read Eval Print Loop
- run Run a program given a filename or url to the module
- test Run tests
- types Print runtime TypeScript declarations
- upgrade Upgrade deno executable to newest version
-
- ENVIRONMENT VARIABLES:
- DENO_DIR Set deno's base directory (defaults to $HOME/.deno)
- DENO_INSTALL_ROOT Set deno install's output directory
- (defaults to $HOME/.deno/bin)
- NO_COLOR Set to disable color
- HTTP_PROXY Proxy address for HTTP requests
- (module downloads, fetch)
- HTTPS_PROXY Same but for HTTPS
Note the SUBCOMMANDS
section in the help, that lists all the commands we can run. What subcommands do we have?
请注意帮助中的“ SUBCOMMANDS
部分,其中列出了我们可以运行的所有命令。 我们有哪些子命令?
bundle
bundle module and dependencies of a project into single file
bundle
束模块和项目的依赖关系到单个文件
cache
cache the dependencies
cache
依赖项
completions
generate shell completions
completions
生成壳补全
doc
show documentation for a module
doc
显示模块的文档
eval
to evaluate a piece of code, e.g. deno eval "console.log(1 + 2)"
eval
评估一段代码,例如deno eval "console.log(1 + 2)"
fmt
a built-in code formatter (similar to gofmt
in Go)
fmt
内置的代码格式化程序(类似于Go中的gofmt
)
help
prints this message or the help of the given subcommand(s)
help
打印此消息或给定子命令的帮助
info
show info about cache or info related to source file
info
显示有关缓存的信息或与源文件相关的信息
install
install script as an executable
install
安装脚本安装为可执行文件
repl
Read-Eval-Print-Loop (the default)
repl
Read-Eval-Print-Loop(默认值)
run
run a program given a filename or url to the module
run
给模块指定文件名或URL的程序
test
run tests
test
运行测试
types
print runtime TypeScript declarations
types
打印运行时TypeScript声明
upgrade
upgrade deno
to the newest version
upgrade
升级deno
最新版本
You can run deno <subcommand> help
to get specific additional documentation for the command, for example deno run --help
.
您可以运行deno <subcommand> help
来获取该命令的特定其他文档,例如deno run --help
。
As the help says, we can use this command to start a REPL (Read-Execute-Print-Loop) using deno
without any other option.
如帮助所述,我们可以使用此命令使用deno
来启动REPL(Read-Execute-Print-Loop),而无需任何其他选择。
This is the same as running deno repl
.
这与运行deno repl
相同。
A more common way you’ll use this command is to execute a Deno app contained in a TypeScript file.
您将使用此命令的更常见方式是执行TypeScript文件中包含的Deno应用程序。
You can run both TypeScript (.ts
) files, or JavaScript (.js
) files.
您可以同时运行TypeScript( .ts
)文件或JavaScript( .js
)文件。
If you are unfamiliar with TypeScript, don’t worry: Deno is written in TypeScript, buf you can write your “client” applications in JavaScript.
如果您不熟悉TypeScript,请不要担心:Deno是用TypeScript编写的,但是您可以使用JavaScript编写“客户端”应用程序。
My TypeScript tutorial will help you get up and running quickly with TypeScript if you want.
如果需要,我的TypeScript教程将帮助您快速入门并使用TypeScript。
Let’s run a Deno app for the first time.
让我们第一次运行Deno应用程序。
What I find pretty amazing is that you don’t even have to write a single line - you can run a command from any URL.
我感到非常惊奇的是,您甚至不必写一行-您可以从任何URL运行命令。
Deno downloads the program, compiles it and then runs it:
Deno下载程序,对其进行编译,然后运行:
Of course running arbitrary code from the Internet is not a practice generally recommendable. In this case we are running it from the Deno official site, plus Deno has a sandbox that prevents programs to do anything you don’t want to allow. More on this later.
当然,通常不建议从Internet运行任意代码。 在这种情况下,我们从Deno官方网站上运行它,另外Deno还有一个沙箱,可以阻止程序执行您不希望做的任何事情。 稍后再详细介绍。
This program is very simple, just a console.log()
call:
该程序非常简单,只需进行console.log()
调用即可:
console.log('Welcome to Deno 本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。