Skip to main content

Node TypeScript Project

How to create a TypeScript project in Node.js

Big thanks to Jordan Irabor for his article "How To Set Up a Node Project With Typescript" that inspired me a lot for this.

#!/bin/bash

# this is because on my laptop:
# - there is the system 'node' (i.e. used by MS Team for Linux)
# - there is the 'node' I use for development work
export NODE_BIN="/home/mihamina/Apps/node/bin"
export PRJ="ts-project-01"

mkdir ${PRJ}
cd ${PRJ}

# We are inside the project directory, now
${NODE_BIN}/npm init -y
${NODE_BIN}/npm install --save typescript

cat << MIHAM > tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  },
  "lib": ["es2015"]
}
MIHAM

${NODE_BIN}/npm install --save express
${NODE_BIN}/npm install --save @types/express

mkdir src

cat << MIHAM > src/app.ts
import express from 'express';
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  return console.log('Express is listening at http://localhost:'+port);
});

MIHAM

${NODE_BIN}/npx tsc
${NODE_BIN}/node dist/app.js