<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: pabath99</title>
    <description>The latest articles on Forem by pabath99 (@pabath99).</description>
    <link>https://forem.com/pabath99</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1151885%2Ff959ad9b-aa00-4797-b3a8-50e7fea70673.jpg</url>
      <title>Forem: pabath99</title>
      <link>https://forem.com/pabath99</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pabath99"/>
    <language>en</language>
    <item>
      <title>A Step-by-Step Guide to Setting Up a Node.js Project with TypeScript</title>
      <dc:creator>pabath99</dc:creator>
      <pubDate>Tue, 13 Feb 2024 08:21:14 +0000</pubDate>
      <link>https://forem.com/pabath99/a-step-by-step-guide-to-setting-up-a-nodejs-project-with-typescript-4dn2</link>
      <guid>https://forem.com/pabath99/a-step-by-step-guide-to-setting-up-a-nodejs-project-with-typescript-4dn2</guid>
      <description>&lt;p&gt;Integrating TypeScript with Node.js enhances the development process by improving code readability and maintainability. While it doesn’t increase runtime efficiency, TypeScript’s static typing helps detect errors early, simplifying the management of complex applications.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites: Install Node.js and Postman
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Before starting, ensure you have Node.js installed on your machine. Download it from the official website: &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;Node.js.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, for API testing, install Postman. You can download it from the official website: &lt;a href="https://dl.pstmn.io/download/latest/win64" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create a new project folder
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Create a new folder for your project and navigate to it using the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir nodejs-typescript-guide
cd nodejs-typescript-guide
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Initialize a new Node.js project
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Run the following command to initialize a new Node.js project and create a &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Configure TypeScript
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Once TypeScript is installed, you’ll need to create and configure the &lt;code&gt;tsconfig.json&lt;/code&gt; file, which is essential for defining compiler options and project settings. To generate a basic &lt;code&gt;tsconfig.json&lt;/code&gt; file, execute the command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After initializing the &lt;code&gt;tsconfig.json&lt;/code&gt; file, it's time to tailor it to your project's needs. Open the &lt;code&gt;tsconfig.json&lt;/code&gt; file and modify it to resemble the configuration below. This example provides a solid foundation for most projects, ensuring strict type-checking, consistent casing in filenames, and compatibility with common module systems:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "rootDir": "./src",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,  
    "strict": true,
    "skipLibCheck": true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;"target": "es2016"&lt;/code&gt;:Sets ECMAScript target version for output JavaScript to ES2016.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"module": "commonjs"&lt;/code&gt;: Use CommonJS module system for module loading compatibility.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"rootDir": "./src"&lt;/code&gt;: Source files located in ./src directory for compilation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"outDir": "./dist"&lt;/code&gt;: Compiled JavaScript output to ./dist directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"esModuleInterop": true&lt;/code&gt;: Enables compatibility with ES6 module imports.&lt;/li&gt;
&lt;li&gt;"&lt;code&gt;forceConsistentCasingInFileNames": true&lt;/code&gt;: Ensures case sensitivity in file names.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"strict": true&lt;/code&gt;: Enables all strict type-checking options.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"skipLibCheck": true&lt;/code&gt;: Skip type checking of declaration files (&lt;code&gt;.d.ts&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Install Express and @types/express @types/node
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Install Express and TypeScript definitions for Express:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express
npm install --save-dev @types/express @types/node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;(Optional) Install the touch-cli globally using npm to create files quickly from the command line&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install touch-cli -g 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Create the Project Structure and Initialize the Express Server Without Types
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
First, create a &lt;code&gt;src&lt;/code&gt; folder and inside it, a &lt;code&gt;server.ts&lt;/code&gt; file for your project. This file will contain the source code for your Express server.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir src
touch src/server.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open the &lt;code&gt;src/server.ts&lt;/code&gt; file and write the initial setup code for an Express server:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Import the 'express' module
import express from 'express';

// Create an Express application
const app = express();

// Set the port number for the server
const port = 3000;

// Define a route for the root path ('/')
app.get('/', (req, res) =&amp;gt; {
  // Send a response to the client
  res.send('Hello, TypeScript + Node.js + Express!');
});

// Start the server and listen on the specified port
app.listen(port, () =&amp;gt; {
  // Log a message when the server is successfully running
  console.log(`Server is running on http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Update &lt;code&gt;package.json&lt;/code&gt; and Run the Project
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Modify your &lt;code&gt;package.json&lt;/code&gt; file to include TypeScript compilation scripts&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node dist/server.js",
  "build": "tsc"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;"start"&lt;/code&gt; script starts the server using the compiled JavaScript file in the 'dist' directory.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;"build"&lt;/code&gt; script builds the TypeScript files to JavaScript using the TypeScript compiler.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Execute the following commands in your terminal to compile the TypeScript code and start your server:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will work, but the code lacks type annotations. Visit &lt;code&gt;http://localhost:3000&lt;/code&gt; in your browser, and you should see "Hello, TypeScript + Node.js + Express!"&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Add types gradually
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
Now, let’s add types incrementally:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Import the 'express' module along with 'Request' and 'Response' types from express
import express, { Request, Response } from 'express';

// Create an Express application
const app = express();

// Specify the port number for the server
const port: number = 3000;

// Define a route for the root path ('/')
app.get('/', (req: Request, res: Response) =&amp;gt; {
  // Send a response to the client
  res.send('Hello, TypeScript + Node.js + Express!');
});

// Start the server and listen on the specified port
app.listen(port, () =&amp;gt; {
  // Log a message when the server is successfully running
  console.log(`Server is running on http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, we’ve added types for the &lt;code&gt;port&lt;/code&gt; variable and the &lt;code&gt;req&lt;/code&gt; (Request) and &lt;code&gt;res&lt;/code&gt; (Response) parameters in the route handler.&lt;/p&gt;

&lt;p&gt;Run the project:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, you have types for the &lt;code&gt;port&lt;/code&gt; variable. Continue adding types to other parts of your code as needed. This approach allows you to gradually introduce TypeScript into your project.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Testing Your Application with Postman
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
After setting up your application, you can use Postman to send requests to your server and test its responses. This is crucial for ensuring your API behaves as expected.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open Postman: Launch the Postman application.&lt;/li&gt;
&lt;li&gt;Create a New Request: Set up a new request in Postman by specifying the request type GET and the endpoint URL &lt;code&gt;http://localhost:3000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Send the Request: Hit send and view the response from your server.&lt;/li&gt;
&lt;li&gt;Analyze the Response: Check the status code, response body, and headers to ensure your API behaves correctly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fasfy35t55h2u7fjvog7e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fasfy35t55h2u7fjvog7e.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 10: Understanding the ‘dist’ Folder
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
The &lt;code&gt;dist&lt;/code&gt; folder is the directory where TypeScript transpiles the &lt;code&gt;.ts&lt;/code&gt; files into &lt;code&gt;.js&lt;/code&gt; files. The &lt;code&gt;dist&lt;/code&gt; folder and &lt;code&gt;server.js&lt;/code&gt; are generated after running the &lt;code&gt;npm run build&lt;/code&gt; command, which compiles the TypeScript code to JavaScript as per the configuration in &lt;code&gt;tsconfig.json&lt;/code&gt;. This folder is not directly created or modified by the developer; it's managed through the build process controlled by the TypeScript compiler.&lt;/p&gt;

&lt;p&gt;That’s the complete guide to setting up a Node.js project with TypeScript. This setup gives you a strong foundation for building robust and maintainable server-side applications.&lt;br&gt;
**&lt;/p&gt;

&lt;h2&gt;
  
  
  Explore the Complete Code
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
If you’d like to explore the complete codebase , you can find it on GitHub: &lt;a href="https://github.com/pabath99/FullStackFables" rel="noopener noreferrer"&gt;https://github.com/pabath99/FullStackFables&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
