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?
What is Deno? First of all, Deno is not a fork of Node - it's a new implementation based on modern features of the JavaScript language, although the name is an anagram of Node. Deno is a secure runtime for JavaScript and TypeScript based on Google's V8 and its core is built in Rust (Node's implementation is in C++).
Some reasons for the disappeared hype In May of this year, not only was the coronavirus in the center of attention — in the JavaScript and backend-development community, Deno spread. The first stable version was there, and already a huge hype broke out.
We can also say that Deno is the improved version of Node, which is a faster, secure, and modern tool to build apps.
An experienced Node. js developer will be able to pick up Deno and start running with it almost straight away. Deno is worth playing around with if you're already an experienced Node. js developer because you might learn some new concepts and have a small advantage when Deno starts to become popular.
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.
ts-node relies on the Node.js runtime so it is fair to include it here:
Deno is itself a runtime so it doesn't use anything else:
GitHub:
Stack Overflow:
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).
There are 1256 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.
typescript
and ts-node
with their dependencies with npm
npm install typescript ts-node
node_modules
tsc
and run with node
(because it is under the hood)import
files using relative paths (usually without .ts
suffix)import
the dependencies installed with npm
(or yarn
) in node_modules
import
files using relative paths (always with .ts
suffix!)import
URLs directly from the Web (no need for npm install
)Here is an example of publishing a minimal library written in TypeScript and using it.
This is what I am doing right now with an example project on:
https://github.com/rsp/node-ts-hello
Creating library:
package.json
with npm init
npm install typescript
package-lock.json
in the repo (there are pros and cons)src
dir where you will keep TypeScript fileshello.ts
to src
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 without dist
in .npmignore
"declaration": true
so you have *.d.ts
files generated"main": "dist/hello.js"
in package.json
(note the "js" suffix)"types": "dist/hello.d.ts"
in package.json
(note the "ts" suffix)"build": "tsc"
to package.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 with npm 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 with npm 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) or ts-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.
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:
$ 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.)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With