Skip to main content

Complete Setup Guide for a Node.js Server with TypeScript and Nodemon

Updated 4 min readMarkdown
Complete Setup Guide for a Node.js Server with TypeScript and Nodemon

If you’re diving into backend development with Node.js and want the benefits of TypeScript’s type safety and nodemon for automatic server restarts during development, this guide is for you. By the end of this post, you’ll have a fully functional setup that enables efficient coding without the hassle of manual recompilation.

What is Node.js?

Node.js is a runtime environment that allows you to run JavaScript code outside the browser. It’s built on Google Chrome’s V8 engine, making it incredibly fast and efficient. With Node.js, you can:

  • Build server-side applications.

  • Create APIs to interact with databases or other services.

  • Develop real-time applications like chat apps or gaming platforms.

Step 1: Download and Install Node.js

  1. Go to the Node.js official website: https://nodejs.org.

  2. You’ll see two versions:

    • LTS (Long-Term Support): Recommended for most users, especially for production use.

    • Current: Includes the latest features but may not be as stable.

  3. Download the LTS version for your operating system and install it.

Step 2: Verify Installation

After installation, open a terminal and run the following commands to verify that Node.js and npm (Node Package Manager) are installed:

node -v
npm -v

You should see version numbers for both. For example:

v18.17.0
8.15.0

Why Use TypeScript with Node.js?

TypeScript enhances your JavaScript code by adding optional static typing. This leads to fewer runtime errors and better maintainability. Coupled with nodemon, which automatically reloads your server when changes are detected, this setup is a developer’s dream.

Step 3: Set Up a Node.js Project

1. Create a New Directory

Create a folder for your project and navigate to it in the terminal:

mkdir nodejs-setup
cd nodejs-setup

2. Initialize a New Node.js Project
Run the following command to create a package.json file, which will manage your project’s dependencies:

npm init -y

The -y flag automatically accepts the default configuration.

Setting Up TypeScript and Nodemon

Now that Node.js is installed and initialized, let’s set up TypeScript and nodemon for a better development experience.

Step 1: Install Dependencies

Install Runtime Dependencies

We’ll use Express (a web framework) and other utilities:

npm install express body-parser cors dotenv

Install Development Dependencies

These help during development but are not needed in production:

npm install --save-dev typescript ts-node nodemon @types/node @types/express

Step 2: Configure TypeScript

  1. Initialize TypeScript: Run the following command to create a tsconfig.json file:

     npx tsc --init
    
  2. Update the tsconfig.json file: Replace its content with the following:

     {
       "compilerOptions": {
         "target": "ES6",
         "module": "commonjs",
         "rootDir": "./src",
         "outDir": "./dist",
         "strict": true,
         "esModuleInterop": true,
         "skipLibCheck": true
       },
       "include": ["src"],
       "exclude": ["node_modules"]
     }
    
  3. Update the package.json file: Replace its content with the following:

     {
       "name": "server",
       "version": "1.0.0",
       "scripts": {
         "test": "echo \"Error: no test specified\" && exit 1",
         "dev": "nodemon src/index.ts"
       },
       "keywords": [],
       "author": "",
       "license": "ISC",
       "description": "",
       "dependencies": {
         "cors": "^2.8.5",
         "dotenv": "^16.4.7",
         "express": "^4.21.2",
       },
       "devDependencies": {
         "@types/express": "^5.0.0",
         "nodemon": "^3.1.9",
         "ts-node": "^10.9.2"
       }
     }
    

Step 3: Create the Server Code

  1. Create a Source Folder:

     mkdir src
     touch src/index.ts
    
  2. Write Your First Server Code: Open src/index.ts and add:

     import express from "express";
    
     const app = express();
     const PORT = process.env.PORT || 3000;
    
     app.use(express.json());
    
     app.get("/", (req, res) => {
         res.send("Hello, World! Welcome to your Node.js server with TypeScript.");
     });
    
     app.listen(PORT, () => {
         console.log(`Server is running on http://localhost:${PORT}`);
     });
    

Step 4: Run Your Server

Start your server with:

npm run dev

Open a browser and go to http://localhost:3000. You’ll see:

Hello, World! This is your TypeScript server.

Conclusion

Setting up a Node.js server with TypeScript and nodemon Although it may seem challenging initially, it’s an invaluable skill for developers. This setup ensures you can code efficiently, avoid common runtime errors, and focus on building robust applications.

That concludes this topic. Thank you for reading!

If you found this article helpful, please consider liking and sharing it with others. If you have any questions, feel free to comment; I will do my best to provide a helpful response.

Connect with me 👋

About the author

  • Web developer specializing in React and it's ecosystem with one year of experience working in startups. Ability to translate business requirements into innovative software solutions.

No comments yet. Be the first to comment.