<?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>DEV Community: ROHANYH101</title>
    <description>The latest articles on DEV Community by ROHANYH101 (@rohanyh101).</description>
    <link>https://dev.to/rohanyh101</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%2F1696477%2Fb4bba8a5-88ba-4625-a88c-7ddac8bf69db.jpg</url>
      <title>DEV Community: ROHANYH101</title>
      <link>https://dev.to/rohanyh101</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohanyh101"/>
    <language>en</language>
    <item>
      <title>TypeScript Node.js Boilerplate</title>
      <dc:creator>ROHANYH101</dc:creator>
      <pubDate>Fri, 28 Jun 2024 02:11:56 +0000</pubDate>
      <link>https://dev.to/rohanyh101/typescript-nodejs-boilerplate-1fpm</link>
      <guid>https://dev.to/rohanyh101/typescript-nodejs-boilerplate-1fpm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzseei9bskc358yxcqnd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzseei9bskc358yxcqnd.png" alt="tsnode" width="600" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When developing a Node.js application, integrating TypeScript can significantly enhance your coding experience Combining these benefits with tools like nodemon and ts-node can streamline your development workflow. Here's a comprehensive guide to setting up a Node.js project with TypeScript, nodemon, and ts-node.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Initialize Your Project
&lt;/h2&gt;

&lt;p&gt;Start by creating a new directory for your project and initializing it with npm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-node-ts-project
&lt;span class="nb"&gt;cd &lt;/span&gt;my-node-ts-project
npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Install TypeScript and ts-node
&lt;/h2&gt;

&lt;p&gt;Next, install TypeScript and ts-node as development dependencies. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript, and ts-node is a utility that allows you to run TypeScript directly in Node.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;typescript ts-node &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Install Nodemon
&lt;/h2&gt;

&lt;p&gt;Nodemon is a tool that helps develop Node.js applications by automatically restarting the node application when file changes are detected. Install nodemon as a development dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;nodemon &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Configure TypeScript
&lt;/h2&gt;

&lt;p&gt;then run the below command to generate the default tsconfig.json. This file contains the configuration for the TypeScript compiler.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;will look something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Set Up Project Structure
&lt;/h2&gt;

&lt;p&gt;Create the project structure with a source directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;src
&lt;span class="nb"&gt;touch &lt;/span&gt;src/index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Write a Sample TypeScript Code
Add some basic TypeScript code in src/index.ts to test the setup.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;src/index.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greeting = (name: string): string =&amp;gt; {
  return `Hello, ${name}!`;
};

console.log(greeting('World'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Configure Script,
&lt;/h2&gt;

&lt;p&gt;Configure &lt;code&gt;package.json&lt;/code&gt; to include scripts for running your TypeScript code using ts-node and nodemon.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "scripts": {
    "start": "ts-node src/index.ts",
    "dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;start: This script runs your TypeScript file directly using ts-node.&lt;br&gt;
dev: This script uses nodemon to watch for file changes in the src directory and automatically restarts the application using ts-node.&lt;/p&gt;
&lt;h2&gt;
  
  
  8. Run Your Application
&lt;/h2&gt;

&lt;p&gt;Now, you can start your application in development mode with:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Nodemon will watch for any changes in your TypeScript files and automatically restart the application, making the development process smooth and efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Setting up a Node.js project with TypeScript, nodemon, and ts-node enhances your development workflow by providing type safety, real-time updates, and modern JavaScript features.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;For a complete example and more details, you can refer to my GitHub repository: &lt;a href="https://github.com/rohanyh101/TypeScript-Node.js-Boilerplate"&gt;rohanyh101&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to clone the repository and explore the setup in detail!&lt;/p&gt;

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