Skip to content

Node.js and TypeScript Setup

TypeScript in Node.js requires a bit of configuration — Node.js doesn’t understand TypeScript natively. This lesson sets up the project structure used for the Bulletin API.

Option 1: Compile first — Use tsc to compile TypeScript to JavaScript, then run the JavaScript. Good for production builds.

Option 2: tsx (recommended for development)tsx is a tool that runs TypeScript directly without a separate compile step. Faster for development.

This course uses tsx for development and tsc for production builds.

Terminal window
mkdir bulletin-api
cd bulletin-api
npm init -y

Install TypeScript and tsx:

Terminal window
npm install --save-dev typescript tsx @types/node
Terminal window
npx tsc --init

Update tsconfig.json with Node.js-appropriate settings:

{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}

Key settings:

  • "module": "NodeNext" — use Node.js’s native ES module resolution
  • "outDir": "./dist" — compiled output goes here
  • "rootDir": "./src" — source files live here
  • "strict": true — full TypeScript strictness
bulletin-api/
├── src/
│ ├── index.ts ← entry point
│ ├── routes/ ← Express route handlers
│ ├── middleware/ ← custom middleware
│ ├── db/ ← database setup and queries
│ └── types/ ← shared TypeScript types
├── tsconfig.json
└── package.json

Update package.json:

{
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"start": "node dist/index.js"
}
}
  • npm run dev — runs with tsx watch (auto-restarts on file changes, like Nodemon)
  • npm run build — compiles TypeScript to dist/
  • npm start — runs the compiled JavaScript
src/index.ts
const message: string = 'Bulletin API starting...'
console.log(message)
console.log('Node.js version:', process.version)

Run it:

Terminal window
npm run dev

You should see the output. Change the message and watch it restart automatically.

  1. Follow the setup above to create a bulletin-api project directory.
  2. Install the dependencies and configure tsconfig.json.
  3. Create src/index.ts with a typed variable and run npm run dev.
  4. Change the message in the file — confirm the server restarts and shows the new output.
  • tsx runs TypeScript files directly — no separate compile step needed during development.
  • tsconfig.json with "module": "NodeNext" is the correct setup for modern Node.js.
  • Source goes in src/, compiled output goes in dist/.
  • npm run dev uses tsx watch for auto-restart; npm start runs the compiled build.