Tech Tips
  • How to
  • Top 10
  • Interview Tricks
  • Java
  • Programing
No Result
View All Result
  • How to
  • Top 10
  • Interview Tricks
  • Java
  • Programing
No Result
View All Result
Tech Tips
No Result
View All Result
Home Technology

How to set up a TypeScript and JavaScript Project Using Nodemon

Comprehensive Guide to Setting Up a JavaScript and TypeScript Project

Dineshkumar by Dineshkumar
22 august 2024
in Technology, How to, Programing
150 1
0
Basic of Javascript

TypeScript and JavaScript project using Nodemon

469
SHARES
1.5k
VIEWS
Share on FacebookShare on Twitter

Learn how to set up a TypeScript and JavaScript project using Nodemon. This comprehensive guide covers initialization, TypeScript configuration, and automating tasks with Nodemon for a smooth development experience.This guide will walk you through setting up a project using JavaScript and TypeScript, from initialization to handling common errors

Before Going to Start you Install Node.js on your local System

Table of Contents

Toggle
  • How to Set up a TypeScript and JavaScript Project Using Nodemon
  • Step 1: Initialize the Javascript Project 
      • npm init: Setting Up Node.js
      • Installing Nodemon for Auto-reload
  • Step 2: Setting Up TypeScript
      • Why Use TypeScript?
      • npm init -y: Streamlining the Setup Process
    • Creating Project Structure
  • Step 3: Installing TypeScript
      • 1. Installing TypeScript as a Development Dependency
      • Create two Folder Dist and Src in the Typescript Project 
  • Step 4: Installing Nodemon
      • Why Use Nodemon?
      • Installing and Configuring Nodemon in package.json
  • Step 5: Finally Run the Code npm run start

How to Set up a TypeScript and JavaScript Project Using Nodemon

In today’s rapidly evolving tech landscape, setting up a project with both JavaScript and TypeScript is a common practice. TypeScript, a superset of JavaScript, brings static typing to your project, making it easier to catch errors early and improve code maintainability.

Step 1: Initialize the Javascript Project 

npm init: Setting Up Node.js

The first step in setting up your project is initializing a Node.js project. This is done using the npm init command, which creates a package.json file that keeps track of your project’s dependencies, scripts, and metadata.

npm init

This command will prompt you with several questions about your project. Answer them accordingly, and npm will generate a package.json file.

Installing Nodemon for Auto-reload

Nodemon is a tool that automatically restarts your Node.js application when file changes are detected. This is particularly useful during development.

npm install nodemon --save-dev

Create a src folder in your project directory. This folder will hold all your Javascript files.

Inside the package.json file, you need to add this code in the script

"start": "nodemon --exec \"cls && node\" ./src/index.js -q"

{
  "name": "javascript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec \"cls && node\" ./src/index.js -q"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^3.1.4"
  }
}

The script "start": "nodemon --exec \"cls && node\" ./src/index.js -q" added to the package.json file is used to define a custom start command for your Node.js application. Here’s a brief explanation:

  1. nodemon: This is the tool that watches your files for changes and automatically restarts your Node.js application when it detects a change.
  2. --exec: This option allows you to specify a command to execute when restarting your application.
  3. "cls && node":
    • cls: This clears the console before running your application, providing a clean output each time.
    • node: This is the command to start the Node.js application.
  4. ./src/index.js: This specifies the path to the main file of your application that Nodemon should run.
  5. -q: This stands for “quiet” mode, which reduces the amount of output Nodemon provides in the console.

Step 2: Setting Up TypeScript

Why Use TypeScript?

TypeScript enhances JavaScript by adding static types, which help catch errors during development rather than at runtime. This is especially beneficial for larger projects where maintaining code quality is crucial.

npm init -y: Streamlining the Setup Process

To streamline the setup process, you can use the -y flag with npm init, which automatically answers all prompts with default values.

npm init -y

Creating Project Structure

Creating the src Folder for TypeScript Code

Create a src folder in your project directory. This folder will hold all your TypeScript files.

Creating the dist Folder for JavaScript Output

Similarly, create a dist folder where the compiled JavaScript files will be output.

Step 3: Installing TypeScript

1. Installing TypeScript as a Development Dependency

To work with TypeScript, you must install it as a development dependency.

npm install -g typescript

Navigate to Your Project Directory:

Open your terminal and navigate to the root directory of your project where you want to create the tsconfig.json file.

Create two Folder Dist and Src in the Typescript Project 

Inside the Src create one typescript file index.ts

Creating and Running TypeScript Files

console.log("Hello, TypeScript!");

Generate tsconfig.json:

Run the following command in the terminal:

tsc --init

This command will create a tsconfig.json file in your project directory with a basic configuration.

Customize tsconfig.json:

After generating the file, you can open it in a text editor and customize it according to your project’s needs. It includes various options for configuring the TypeScript compiler, such as specifying the target JavaScript version, module system, and more.

The generated tsconfig.json file will provide a solid starting point for managing your TypeScript project’s configuration.

typescript

Step 4: Installing Nodemon

Why Use Nodemon?

Nodemon watches for changes in your files and automatically restarts the Node.js application, saving you the hassle of manual restarts.

Installing and Configuring Nodemon in package.json

After installing Nodemon, you can configure it to watch TypeScript files and automatically recompile them.

{
  "name": "javascript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec \"cls && tsc && node ./dist/index.js\" ./src/index.ts -q"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^3.1.4"
  }
}

Step 5: Finally Run the Code npm run start

  • NPM looks for the "start" script in the package.json file.
  • It executes the command associated with "start", which in your case is:
    bash
    "start": "nodemon --exec \"cls && tsc && node ./dist/index.js\" ./src/index.ts -q"
  • Nodemon is used to start the application and watch for any changes in your files.
  • cls clears the console.
  • node runs the ./src/index.ts file, which is the entry point of your application and convert the js file and store on dist.
  • The -q flag makes Nodemon run in quiet mode, reducing the output in your terminal.

In short, npm run start initiates your Node.js application with auto-reload capabilities provided by Nodemon.

Tags: javascripttypescript
Previous Post

Basic Spring Boot Project Rest API

Next Post

Understanding JavaScript Variables : A Comprehensive Guide

Next Post
JavaScript Variables

Understanding JavaScript Variables : A Comprehensive Guide

Lasă un răspuns Anulează răspunsul

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Recommended.

Tips to Get 30TB of Free Google Drive Storage

Tips to Get 30TB of Free Google Drive Storage

29 septembrie 2023
How To Download Pdf From Scribd Without Paying

How To Download Pdf From Scribd Without Paying

31 martie 2024

Subscribe.

Trending.

9 Best Site for Freepik Downloader

Top 9 Best Websites for Freepik Downloader

21 octombrie 2023
How To View Deleted Instagram Account

How To View Deleted Instagram Account

20 martie 2024
How To Check If Someone Blocked You On Telegram

How To Block Someone Telegram Account

18 august 2024
Core Java Interview Topics

Core Java Interview Topics

6 august 2024
Spring Boot Actuator

Spring Boot Actuator: Monitoring Your Application with Essential Endpoints

27 octombrie 2024

About

Tech Tips

SmileyTricks

This site delivers Programming Tips, Top 10 Technology Facts, Educational Resources, the Latest Tech Updates, and How-To Guides on tech topics.

Categorii

  • How to
  • Interview Tricks
  • Java
  • Programing
  • React JS
  • Technology
  • Top 10
  • Tutorial

Etichete

Affiliate Design Engineering Innovation javascript React JS SEO typescript Youtube
  • About Us
  • Contact Us
  • Privacy & Policy
  • Disclaimer
  • Terms & Condition

© 2024 SmileyTricks All rights reversed By SmileUpdates Smileytricks.

No Result
View All Result
  • How to
  • Top 10
  • Interview Tricks
  • Java
  • Programing

© 2024 SmileyTricks All rights reversed By SmileUpdates Smileytricks.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In