Node.js, JavaScript and TypeScript

2022 Nov 12 See all posts

Node.js, JavaScript and TypeScript

Here are some nouns and commands in Node.js's world. I want to show you these conceptions.


First of all, Node.js.

Node.js is a back-end JavaScript runtime environment. Developers write code with JavaScript and put codes in files with .js extension, Node.js can run these .js files and gives developers what they expect. If you are familiar with Java, the relationship between JavaScript and Node.js is very similar to Java and Java Virtual Machine.

You can download Node.js and install it by your prefer way. After the installation, you can use node and npm command.

> node --version

> npm --version

JavaScript and node

There are two different ways to run JavaScript code with Node.js.

First, you can just type node in your terminal and press Enter, then you will enter a command mode and node are waiting for your input. You can type JavaScript language and get output. If you want to quit it, just type process.exit().

> node
Welcome to Node.js v18.12.1.
Type ".help" for more information.
> 1+1
> console.log("hi")
> process.exit()

Second, you can write your code in a .js file, and tell node which file should be run.

There is a demo.js.

let i = 1 + 1;

Then, use node to run demo.js.

> node ./demo.js

TypeScript and tsc

Node.js supports JavaScript, but it does not support TypeScript. This means you can not run .ts files which have TypeScript's specific language features in Node.js. To fix this problem, there is a command tsc. It can covert .ts files to .js files, then Node.js can run .js files.

You can install it by npm, then you can use tsc command.

> npm install -g typescript
> tsc --version
Version 4.8.4

There is a demo.ts.

let i: number = 42;

If you try to use node to run this file, you will get an error. Thus because Node.js does not support TypeScript and its type number.

> node ./demo.ts
let i: number = 42;

SyntaxError: Unexpected token ':'
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.12.1

To fix this, you can use tsc command to compile .ts to .js. Then, you can run demo.js with node.

> ls
> tsc ./demo.ts
> ls
demo.ts    demo.js
> node ./demo.js

TypeScript and ts-node

If we have to use tsc to compile .ts files to .js files every time, we will be very tired. To make it convenient, there is a command ts-node.

You can install it by npm, then you can use ts-node command.

> npm install -g ts-node
> ts-node --version

Then, you can run .ts files with ts-node, directly.

> ts-node ./demo.ts

In conclusion, demo.ts + tsc -> demo.js + node = demo.ts + ts-node.