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 0.3.6
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:
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:
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.
原文 https://stackoverflow.com/questions/53428120/deno-vs-ts-node-whats-the-difference