Table of Contents
Using Vite
The following commands need to be run in the client directory.
Using Create React App
The following commands need to be run inside the server directory we’ve created.
The following content is for the server directory.
The following content is for the project’s root directory
Vite
Create React App
Home Web Front-end JS Tutorial MERN TypeScript Setup Guide

MERN TypeScript Setup Guide

Jan 09, 2025 pm 06:36 PM

MERN TypeScript Setup Guide

Hello, Welcome to my guide on setting up a MERN stack project using TypeScript. This guide is designed to help both beginners and experienced developers to set up a fully functional development environment for MERN stack development.


Now, let’s get started with the setup.

Requirements
  • Node.js (version 14 or above recommended)
  • MongoDB database or MongoDB Community Server
  • Install TypeScript using the following command:
npm install -g typescript
Copy after login
Copy after login
Copy after login

Root
  1. Create a directory for your project with the following command:
<span>mkdir my-app</span>
Copy after login
Copy after login
Copy after login

2. Navigate to the directory:

<span>cd my-app</span>
Copy after login
Copy after login
Copy after login

3. Run the following command to initialize node in our directory.

Explanation: This command initializes a new Node.js project and creates a package.json file with default values.

npm init -y
Copy after login
Copy after login
Copy after login

Client

For the client, you can use either Vite or Create React App. Here are the instructions for both:

Using Vite

  1. On your root directory, type the following command:

Explanation: This command creates a new Vite application in a directory named client.

npx create-vite@latest client
Copy after login
Copy after login

2. You will see different options, select React.
3. Choose the option: TypeScript SWC.
4. Once it finishes, use the following command to navigate to the client directory.

<span>cd client</span>
Copy after login
Copy after login

The following commands need to be run in the client directory.

5. Once in the client directory, run the following command to install all dependencies.

Explanation: This command installs the dependencies listed in the package.json file.

npm install
Copy after login
Copy after login

6. Once installed, navigate to the Project’s root directory using the following command:

<span>cd ..</span>
Copy after login
Copy after login

Using Create React App

  1. On your root directory, type the following command:

Explanation: This command creates a new Create React App application in a directory named client.

npx create-react-app client --template typescript
Copy after login
Copy after login

Server
  1. On your root directory, let’s create the server directory.

Explanation: This command creates a new directory named server.

<span>mkdir server</span>
Copy after login

2. Navigate to the server directory using the following command:

<span>cd server</span>
Copy after login

The following commands need to be run inside the server directory we’ve created.

3. Run this command to automatically create package.json file.

npm <span>init -y</span>
Copy after login

4. Now, run the following commands to install our dependencies.

Explanation: These commands install the necessary dependencies for our server. cors is used for enabling CORS, dotenv for loading environment variables, express for building the server, and mongoose for connecting to MongoDB. The development dependencies include TypeScript and the type definitions for our packages, as well as nodemon and ts-node for running our server during development.

npm install -g typescript
Copy after login
Copy after login
Copy after login

5. Create a .gitignore file and add the following lines to it:

Explanation: The .gitignore file specifies intentionally untracked files that Git should ignore.

<span>mkdir my-app</span>
Copy after login
Copy after login
Copy after login

6. Now, let’s setup TypeScript, create a file named tsconfig.json

Explanation: The tsconfig.json file is a configuration file for TypeScript. It specifies the root files and the compiler options to compile the project.

7. Copy and Paste the following configurations:

<span>cd my-app</span>
Copy after login
Copy after login
Copy after login

8. Create a directory named src inside your server directory using the following command:

npm init -y
Copy after login
Copy after login
Copy after login

The following commands need to be run inside the src directory we’ve created.

9. Create a file called server.ts

Explanation: This server.ts file sets up an Express server that connects to a MongoDB database and starts listening on a specified port.

10. Here’s a basic implementation of what the server.ts file should look like:

npx create-vite@latest client
Copy after login
Copy after login

11. Let’s head back to the server directory using the following command:

<span>cd client</span>
Copy after login
Copy after login

Explanation: This command changes the current directory to the parent directory.

The following content is for the server directory.

12. Now, let’s head over to our package.json file in the server directory.

13. Let’s add a command so that we can run nodemon with ts-node for our development.

14. Copy and paste this line on the script part of your package.json:

npm install
Copy after login
Copy after login

15. Once this is done, let’s head over to the root directory with the following command:

<span>cd ..</span>
Copy after login
Copy after login

Running our project

The following content is for the project’s root directory

  1. Run the following command to install the development dependency concurrently.

Explanation: concurrently is a package that allows you to run multiple npm scripts concurrently (at the same time).

npx create-react-app client --template typescript
Copy after login
Copy after login

2. Navigate to the package.json file in the root directory. This file contains metadata about your project and its dependencies.

3. Depending on the tool you used to create your client (either Vite or Create React App), copy the corresponding code block and paste it into the scripts section of your package.json file.

Vite

npm install -g typescript
Copy after login
Copy after login
Copy after login

Create React App

<span>mkdir my-app</span>
Copy after login
Copy after login
Copy after login

4. Now, let’s run our project using the following command:

<span>cd my-app</span>
Copy after login
Copy after login
Copy after login

5. Now, our client and server should be running concurrently. You should see the following output in your terminal:

npm init -y
Copy after login
Copy after login
Copy after login

The above is the detailed content of MERN TypeScript Setup Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Zustand asynchronous operation: How to ensure the latest state obtained by useStore? Zustand asynchronous operation: How to ensure the latest state obtained by useStore? Apr 04, 2025 pm 02:09 PM

Data update problems in zustand asynchronous operations. When using the zustand state management library, you often encounter the problem of data updates that cause asynchronous operations to be untimely. �...

See all articles