deno vs ts-node:有什么区别 [英] deno vs ts-node : what's the difference
问题描述
我正在处理一个相对较大的打字稿项目,正在使用 ts-node
来运行节点测试和示例。据我了解, ts-node
会将 ts
文件编译为 js
文件并执行。
最近我听说了 deno
,它是一个打字稿运行时。我在typescript中尝试了一些示例,该示例使用 ts-node
可以工作。我使用 deno
运行示例,控制台中打印了许多编译消息,然后执行代码。后来我发现 /username/.deno
中有缓存文件。我觉得 deno
执行的速度不快于 ts-node
似乎 deno
和 ts-node
都将编译并使用缓存运行。它们之间有什么区别?
TL; DR
Deno它比ts-node更像Node,即它是基于V8的JS运行时。与Node不同,Deno包含TypeScript编译器。 Deno不是Node / npm生态系统的一部分。
ts-node是一个Node.js模块,它使用TypeScript编译器来转换TypeScript代码并运行它在节点中。 ts-node是Node / npm生态系统的一部分。
Deno很快。见下文。
Deno和ts节点相似性
- 它们都运行TypeScript代码
- 它们都可以在Linux,Mac和Windows上运行(但ts节点也可以在SmartOS和AIX上运行)
- 它们都使用Google V8 JavaScript引擎(ts-node通过引擎盖下使用的节点)
Deno和ts节点的差异
ts-node
- ts-node是Node.js模块
- 它是用Node.js编写的。
- 它与npm一起安装
- 它使用TypeScript编译器作为对等依赖项
- 它安装自己的依赖项
- 作为运行时,它使用使用libuv用C ++编写的Node
Deno
- deno是独立的可执行文件
- t使用Node.js
- 它作为单个二进制文件分发
- 它包含TypeScript编译器作为V8快照
- i t没有依赖项
- 这是使用Tokio用Rust编写的运行时
成熟度
ts-node
ts-node依赖于Node.js运行时,因此可以在这里包括它:
- Node.js于2009年发布,最新的LTS版本是10.15.3
- npm于2010年发布,Node LTS中包含的版本为6.4.1
- ts-node于2015年发布,最新版本为8.0.3
Deno
Deno本身是一个运行时,因此不使用其他任何东西:
- Deno已于2018年发布,最新版本为1.0
堆栈溢出:
- 标记为'node的问题.js':270,111
- 标记为 typescript的问题: 78,592
- 标记为 ts-node的问题:94
- 标记为 deno的问题:9
天秤座s
ts-node
您可以使用npm上所有可用的Node库
(目前npm上有955,263个程序包,不是全部用于Node,但仍然很多)
可在以下位置使用的Node库即使npm最初是用TypeScript编写的,通常也会以转换为JavaScript的形式发布,并在 *。d.ts
文件(包含在npm软件包中或已安装)中附加类型定义与 @types
命名空间分开)。
Deno
https://deno.land/x/ 上有55个第三方模块和56个库和
Deno库只是TypeScript文件。
安装差异
ts-node
- 您安装Node.js
- https://nodejs.org/en/download/
- Mac和Linux上的v10(当前LTS)在4500个文件中约为65MB
- 您安装
类型脚本
和ts-node
及其依赖项和npm
-
npm安装打字稿ts-node
- 它将安装10个npm模块,并将212个文件中的44MB放入
node_modules
-
Deno
- 您下载单个二进制文件
- https://github.com/denoland/deno/releases
- 在Mac和Linux上,v0.3.6的未压缩二进制文件在Windows上分别约为47MB和41MB
$ b
ts节点
- 您的代码工作相同好像它是用
tsc
进行转译并用node
运行的(因为它在引擎盖下) - 您可以使用Node API
- 您可以使用所有内置的Node模块
- 您可以使用npm中的模块
- 您可以使用相对路径(通常不带
.ts
后缀)导入
文件 - 您可以
import
随npm
(或<$ cnode_modules
中的$ c> yarn ) Deno
- 您的代码与Node中的代码不同(因为它未与Node一起运行)
- 您使用Deno API
- 您可以使用Deno内置模块
- 您可以使用其他可用的Deno模块
- 您可以使用相对路径(始终带有
.ts
后缀)导入
文件ix!) - 您可以直接从Web上
导入
URL(无需npm安装
)
示例
以下是发布示例
使用Node和ts-node创建和使用TypeScript库
这是我现在在以下示例项目上正在做的事情:
https://github.com/rsp/node-ts-hello
创建库:
- 在npm上找到一个免费的名称(不再足够,请参见下文)
- 在GitHub上创建存储库
- 使用
npm init
$ b $创建 - 使用
npm install Typescript
安装TypeScript编译器 - 确定是否保留
package-lock回购中的.json
(有优缺点) - 创建ea
src
dir,您将在其中保存TypeScript文件 - 添加
hello.ts
到src
- 添加
tsconfig.json
文件并确保: b
$ b- 将
src / ** / *
添加到include
- 将依赖项和您自己的类型添加到
路径
- 添加
outDir: dist
将JS文件放在已知位置 - 添加
dist
目录到.gitignore
,以便编译后的文件不在git - 中,与
.gitignore
,但.npmignore $ c中的无
dist
$ c>
(否则,您将不会发布最重要的文件,请参见下文) - 添加
声明:true
,因此您生成了*。d.ts
文件
- 将
- 在
package.json
中添加main: dist / hello.js
(注意后缀 js ) - 添加
类型:
(请注意后缀 ts)package.json
中的 dist / hello.d.ts - 向
package.json
添加build: tsc
文件,请参见下文) - 使用
npm登录
登录(您不应该一直登录-请参阅:现在正在推广恶意软件:NPM软件包开发人员登录受到编码人员欢迎的黑客入侵工具的困扰) - 使用
npm run build编译项目
- 使用<$ c $发布程序包c> npm发布
- 当您获得
npm ERR!发布失败的PUT 401
您需要使用npm登录
- 登录,当您获得
npm ERR!发布失败的PUT 403
您的软件包可能与现有软件包太相似-尝试在package.json中重命名,重命名存储库并将所有lik更新为自述文件,然后发出itp。在package.json
- 当您获得
- 从npm注销并以
npm注销
- 查看您的
〜/ .npmrc
并确保您没有剩下的像这样的东西:
-
// registry.npmjs.org /:_ authToken = ...
-
package.json
b 在其他项目中使用 ts-node
- 创建一个新目录
- 使用<$ c $创建一个
package.json
文件c> npm init
- (以便您可以在本地为新程序安装依赖项)
- 使用
npm install node-ts-hello
- 安装我们的库可选地使用
npm安装ts-node安装打字稿ts-node
- (除非已全局安装)
- 添加
hi.ts
文件,该文件用于通过以下方式导入我们的库:
-
从 node-ts-hello导入{hello};
-
hello('TS');
-
- 使用
npx ts-node hi.ts
(如果ts-node是本地安装)或ts-node hi.ts (如果ts节点已全局安装)
- 如果遇到错误,请参见下文
潜在的问题:我简化了一点,创建该库的实际过程描述为此处。
使用Deno创建和使用TypeScript库
这是我现在在以下示例项目上所做的事情:
https://github.com/rsp/deno-hello
创建库:
- 在GitHub上创建存储库
- 放入
hello。 ts
仓库中
使用库:
- 创建文件
hi.ts
,其内容为:
-
从 https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts导入{hello};
-
hello('TS');
-
- 使用
deno run hi.ts
运行程序运行将打印:
$ deno运行hi.ts
编译文件:/// Users / rsp / talks / deno / hello-deno-test / hi.ts
下载https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts
编译https://raw.githubusercontent。 com / rsp / deno-hello / master / hello.ts
您好,TS!
第二次运行:
$ deno run hi.ts
您好,TS!
如果更改 hi.ts
重新编译,但依赖项将不会再次下载:
$ deno run hi.ts
编译文件:// /users/rsp/talks/deno/hello-deno-test/hi.ts
您好,TS!
(请注意, touch hi.ts
还不够,您需要进行实际更改,因为Deno会检查文件校验和,而不是时间戳。)
Speed
ts-node
从以下位置启动 hi.ts
的ts节点版本的速度上面的示例:
$ time npx ts-node hi.ts
您好,TS!
真实0m0.904s
用户0m1.300s
sys 0m0.083s
这是在依赖项已经安装并且运行了几次之后,以确保所有缓存都可以正常工作。
差不多一秒钟。
Deno
启动我们的<$的Deno版本的速度以上示例中的c $ c> hi.ts :
$时间表示运行hi.ts
您好,TS!
实际0m0.028s
用户0m0.010s
sys 0m0.015s
这也是在已经安装了依赖项并且运行了几次之后以确保所有缓存都能正常工作。
32倍以上的速度
摘要
Deno应该与Node进行比较,而不是与 ts- node
,因为Deno是全新的运行时,而 ts-node
是Node的模块,因此您的程序使用 ts- node
确实使用Node运行时。
这是一个非常年轻的项目,但是已经吸引了很多人。它没有Node那样多的文档或库,但它意味着它可能是参与其中的最佳时机,因为当它变得更流行时,我认为由于很多原因,这将超出此答案的范围。
程序的启动速度已经非常可观,我希望在那里有更多的改进。 / p>
使用单个文件的开发速度,而无需配置 package.json
或 node_modules
以及直接从URL(如前端)导入依赖项的可能性,将使最终用户代码和库可以以不同的方式工作。我们将看到它在实践中如何工作,但看起来已经很有希望。
I'm working on a relative large typescript project, I'm using ts-node
to run node testing and examples. As far as I understand, ts-node
will compile ts
files to js
files and execute.
Recently I heard about deno
, which is a typescript runtime. I tried a few examples in typescript, which works using ts-node
. I ran the example with deno
, there were many compile messages printed in the console, then execute the code. And later I found there's cache files in /username/.deno
. I don't feel the deno
execution is faster than ts-node
It seems both deno
and ts-node
will compile and run using cache. What's the difference between them?
TL;DR
Deno is more like Node than like ts-node, i.e. it is a JS runtime based on V8. Unlike Node, Deno contains the TypeScript compiler. Deno is not part of the Node/npm ecosystem.
ts-node on the other hand is a Node.js module that uses the TypeScript compiler to transpile TypeScript code and run it in Node. ts-node is part of the Node/npm ecosystem.
Deno is fast. See below.
Deno and ts-node similarities
- They both run TypeScript code
- They both run on Linux, Mac and Windows (but ts-node also on SmartOS and AIX)
- They both use the Google V8 JavaScript engine (ts-node via node that it uses under the hood)
Deno and ts-node differences
ts-node
- ts-node is a Node.js module
- it is written in Node.js
- it's installed with npm
- it uses the TypeScript compiler as a peer dependency
- it installs its own dependencies
- as a runtime it uses Node which is written in C++ using libuv
Deno
- deno is a standalone executable
- it doesn't use Node.js
- it is distributed as a single binary
- it contains the TypeScript compiler as a V8 snapshot
- it has no dependencies
- it is a runtime written in Rust using Tokio
Maturity
ts-node
ts-node relies on the Node.js runtime so it is fair to include it here:
- Node.js was released in 2009, the latest LTS version is 10.15.3
- npm was released in 2010, version included in Node LTS is 6.4.1
- ts-node was released in 2015, the latest version is 8.0.3
Deno
Deno is itself a runtime so it doesn't use anything else:
- Deno was released in 2018, the latest version is 1.0 check latest version here
Popularity
GitHub:
Stack Overflow:
- Questions tagged 'node.js': 270,111
- Questions tagged 'typescript': 78,592
- Questions tagged 'ts-node': 94
- Questions tagged 'deno': 9
Libraries
ts-node
You can use all Node libraries available on npm
(currently there are 955,263 packages on npm, not all of them for Node but still a lot)
The Node libraries that are available on npm even if they were originally written in TypeScript are usually published in a form transpiled to JavaScript with additional type definitions in *.d.ts
files (included in the npm package or installed separately from the @types
namespace).
Deno
There are 55 third-party modules on https://deno.land/x/ and 56 libraries and tools on https://github.com/denolib/awesome-deno#modules (I didn't check if all are the same)
The Deno libraries are just TypeScript files.
Installation difference
ts-node
- you install Node.js
- https://nodejs.org/en/download/
- the v10 (current LTS) on Mac and Linux is around 65MB in 4500 files
- you install
typescript
andts-node
with their dependencies withnpm
npm install typescript ts-node
- it installs 10 npm modules and puts 44MB in 212 files into
node_modules
Deno
- you download a single binary
- https://github.com/denoland/deno/releases
- the uncompressed binary of v0.3.6 on Mac and Linux is around 47MB and 41MB on Windows
Your code differences
ts-node
- your code works the same as if it were transpiled with
tsc
and run withnode
(because it is under the hood) - you can use the Node API
- you can use all built-in Node modules
- you can use modules from npm
- you can
import
files using relative paths (usually without.ts
suffix) - you can
import
the dependencies installed withnpm
(oryarn
) innode_modules
Deno
- your code doesn't work the same as in Node (because it isn't run with Node)
- you use the Deno API
- you can use the Deno built-in modules
- you can use other Deno modules that are available
- you can
import
files using relative paths (always with.ts
suffix!) - you can
import
URLs directly from the Web (no need fornpm install
)
Examples
Here is an example of publishing a minimal library written in TypeScript and using it.
Creating and using a TypeScript library with Node and ts-node
This is what I am doing right now with an example project on:
https://github.com/rsp/node-ts-hello
Creating library:
- find a name that is free on npm (no longer enough, see below)
- create repo on GitHub
- create
package.json
withnpm init
- install TypeScript compiler with
npm install typescript
- decide if you're keeping
package-lock.json
in the repo (there are pros and cons) - create a
src
dir where you will keep TypeScript files - add
hello.ts
tosrc
- add
tsconfig.json
file and make sure to:- add
"src/**/*"
to"include"
- add dependencies and your own types to
"paths"
- add
"outDir": "dist"
to put the JS files in a known place - add the
dist
directory to.gitignore
so that compiled files are not in git - add the same as in
.gitignore
but withoutdist
in.npmignore
(or otherwise you will not publish the most important files, see below) - add
"declaration": true
so you have*.d.ts
files generated
- add
- add
"main": "dist/hello.js"
inpackage.json
(note the "js" suffix) - add
"types": "dist/hello.d.ts"
inpackage.json
(note the "ts" suffix) - add
"build": "tsc"
topackage.json
(watch out for redundant files, see below) - login with
npm login
(you shouldn't be logged in all the time - see: Now Pushing Malware: NPM package dev logins slurped by hacked tool popular with coders) - compile the project with
npm run build
- publish the package with
npm publish
- when you get
npm ERR! publish Failed PUT 401
you need to login withnpm login
- when you get
npm ERR! publish Failed PUT 403
your package may be "too similar to existing packages" - try renaming it in package.json, rename the repo and update all liks to readme, issues itp. in package.json
- when you get
- logout from npm with
npm logout
- see your
~/.npmrc
and make sure you have nothing like this left://registry.npmjs.org/:_authToken=...
Using the library in other project using ts-node
- create a new directory
- create a
package.json
file withnpm init
- (so that you can install dependencies locally for your new program)
- install our library with
npm install node-ts-hello
- optionally install ts-node with
npm install typescript ts-node
- (unless it's installed globally)
- add
hi.ts
file that imports our library with:import { hello } from 'node-ts-hello';
hello('TS');
- run it with
npx ts-node hi.ts
(if ts-node was installed locally) orts-node hi.ts
(if ts-node was installed globally)- if you get errors, see below
Potential problems: I simplified the above a little bit, my actual process of creating that library is described here.
Creating and using a TypeScript library with Deno
This is what I am doing right now with an example project on:
https://github.com/rsp/deno-hello
Creating library:
- create repo on GitHub
- put
hello.ts
in the repo
Using library:
- Create a file
hi.ts
with the contents:import { hello } from 'https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts';
hello('TS');
- Run your program with
deno run hi.ts
The first run will print:
$ deno run hi.ts
Compiling file:///Users/rsp/talks/deno/hello-deno-test/hi.ts
Downloading https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts
Compiling https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts
Hello, TS!
The second run:
$ deno run hi.ts
Hello, TS!
If you change hi.ts
it will be recompiled but the dependencies will not get downloaded again:
$ deno run hi.ts
Compiling file:///Users/rsp/talks/deno/hello-deno-test/hi.ts
Hello, TS!
(Note that touch hi.ts
will not be enough, you need to make the actual changes because Deno checks the file checksum, not the timestamp.)
Speed
ts-node
The speed of starting the ts-node version of our hi.ts
from the examples above:
$ time npx ts-node hi.ts
Hello, TS!
real 0m0.904s
user 0m1.300s
sys 0m0.083s
This is after the dependencies are already installed and after running several times to make sure that all of the caching works. Almost one second.
Deno
The speed of starting the Deno version of our hi.ts
from the examples above:
$ time deno run hi.ts
Hello, TS!
real 0m0.028s
user 0m0.010s
sys 0m0.015s
This is also after the dependencies are already installed and after running several times to make sure that all of the caching works.
More than 32x speed improvement.
Summary
Deno should be compared more with Node than with ts-node
because Deno is an entirely new runtime while ts-node
is a module for Node so your program run with ts-node
really use the Node runtime.
It is a very young project but has already got a lot of traction. It doesn't have as much documentation or libraries as Node but it means that it may be the best time to get involved because when it gets more popular, and I think it will for many reasons that are beyond the scope of this answer, people who already have experience with it will be needed on the market, like it was with Node.
The program startup speed is already very impressive and I expect more improvements there.
The development speed of using single files with no need for configuration like package.json
or node_modules
together with a possibility to import dependencies directly from URLs (like on the frontend) will make it possible to work in a different way both for the end user code and for the libraries. We'll see how it all works in practice but it already looks promising.
这篇关于deno vs ts-node:有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!