Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Getting Started with Node + Typescript

The steps below will get you started with creating a new NodeJS application that you program using Typescript. The content is mostly straight out of this gitbook if you prefer to read from the original source.

Steps

Create and use an area for a new project

mkdir my-project
cd my-project

Initialize the project with a package.json file

npm init -y

Now setup for typescript

npm install typescript --save-dev
npm install @types/node --save-dev
node ./node_modules/typescript/lib/tsc --init

Run these commands to have the typescript files compile into javascript and then have node application restart whenever a file is saved and compiled.

npm install ts-node --save-dev
npm install nodemon --save-dev

Now edit your scripts attribute in your package.json file to automatically build and run the index.ts file whenever we make a change to it.

"scripts": {
  "start": "npm run build:live",
  "build:live": "nodemon --exec ./node_modules/.bin/ts-node -- ./index.ts"
}

Now you can get started building your node application. The example below will create a process that will print out "hello world" when the process is executed.

echo 'console.log("hello world");' > index.ts
npm start

NodeJS lets you run standalone javascript applications/processes. If you want it to act as a webserver, you have to write the application to listen to the appropriate port and handle the html protocol. Luckily most of the work is done for you.

References

Last updated: 10th November 2021
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch