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.

Node.js

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
v18.12.1

> npm --version
9.1.1

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
2
> console.log("hi")
hi
undefined
> 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;
console.log(i);
console.log("hi");

Then, use node to run demo.js.

> node ./demo.js
2
hi

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;
console.log(i);

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
/Users/user/Documents/demo.ts:1
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
demo.ts
> tsc ./demo.ts
> ls
demo.ts    demo.js
> node ./demo.js
42

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
v10.9.1

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

> ts-node ./demo.ts
42

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