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?
ts-node is based on Node, while Deno is a completely different and new server-side runtime, with design changes on API, module systems, security model, etc. (which reflects better of the post ES6 developments). Also, the TypeScript compiler lives directly inside of the single Deno executable (through V8 snapshots) and thus should have a shorter startup time.
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
Deno and ts-node differences
ts-node
Deno
Maturity
ts-node
ts-node relies on the Node.js runtime so it is fair to include it here:
Deno
Deno is itself a runtime so it doesn't use anything else:
Popularity
GitHub:
Stack Overflow:
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
typescript
andts-node
with their dependencies withnpm
npm install typescript ts-node
node_modules
Deno
Your code differences
ts-node
tsc
and run withnode
(because it is under the hood)import
files using relative paths (usually without.ts
suffix)import
the dependencies installed withnpm
(oryarn
) innode_modules
Deno
import
files using relative paths (always with.ts
suffix!)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:
package.json
withnpm init
npm install typescript
package-lock.json
in the repo (there are pros and cons)src
dir where you will keep TypeScript fileshello.ts
tosrc
tsconfig.json
file and make sure to:"src/**/*"
to"include"
"paths"
"outDir": "dist"
to put the JS files in a known placedist
directory to.gitignore
so that compiled files are not in git.gitignore
but withoutdist
in.npmignore
(or otherwise you will not publish the most important files, see below)
"declaration": true
so you have*.d.ts
files generated"main": "dist/hello.js"
inpackage.json
(note the "js" suffix)"types": "dist/hello.d.ts"
inpackage.json
(note the "ts" suffix)"build": "tsc"
topackage.json
(watch out for redundant files, see below)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)npm run build
npm publish
npm ERR! publish Failed PUT 401
you need to login withnpm login
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.jsonnpm logout
~/.npmrc
and make sure you have nothing like this left://registry.npmjs.org/:_authToken=...
Using the library in other project using
ts-node
package.json
file withnpm init
npm install node-ts-hello
npm install typescript ts-node
hi.ts
file that imports our library with:import { hello } from 'node-ts-hello';
hello('TS');
npx ts-node hi.ts
(if ts-node was installed locally) orts-node hi.ts
(if ts-node was installed globally)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:
hello.ts
in the repoUsing library:
hi.ts
with the contents:import { hello } from 'https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts';
hello('TS');
deno run hi.ts
The first run will print:
The second run:
If you change
hi.ts
it will be recompiled but the dependencies will not get downloaded again:(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: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: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 whilets-node
is a module for Node so your program run withts-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
ornode_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.