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.
Two approaches
Section titled “Two approaches”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.
Project setup
Section titled “Project setup”mkdir bulletin-apicd bulletin-apinpm init -yInstall TypeScript and tsx:
npm install --save-dev typescript tsx @types/nodetsconfig.json for Node.js
Section titled “tsconfig.json for Node.js”npx tsc --initUpdate 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
Project structure
Section titled “Project structure”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.jsonnpm scripts
Section titled “npm scripts”Update package.json:
{ "scripts": { "dev": "tsx watch src/index.ts", "build": "tsc", "start": "node dist/index.js" }}npm run dev— runs withtsx watch(auto-restarts on file changes, like Nodemon)npm run build— compiles TypeScript todist/npm start— runs the compiled JavaScript
Your first TypeScript server file
Section titled “Your first TypeScript server file”const message: string = 'Bulletin API starting...'console.log(message)console.log('Node.js version:', process.version)Run it:
npm run devYou should see the output. Change the message and watch it restart automatically.
Exercise
Section titled “Exercise”- Follow the setup above to create a
bulletin-apiproject directory. - Install the dependencies and configure
tsconfig.json. - Create
src/index.tswith a typed variable and runnpm run dev. - Change the message in the file — confirm the server restarts and shows the new output.
tsxruns TypeScript files directly — no separate compile step needed during development.tsconfig.jsonwith"module": "NodeNext"is the correct setup for modern Node.js.- Source goes in
src/, compiled output goes indist/. npm run devusestsx watchfor auto-restart;npm startruns the compiled build.