- Getting Started
- Adding React and Typescript
- Adding Webpack
- Adding index.html
- Adding Components
- Adding npm scripts
- Run Application
- Next Steps
In this guide, we’ll walk through the process of setting up a React project with TypeScript from scratch using Webpack 5. This setup provides you with a clean, minimal configuration to get started with modern web development.
Getting Started
To begin with, create a folder for our new project:
1 | mkdir react-boilerplate |
Then initialise the project with:
1 | npm init -y |
This will generate a package.json file in the project root.
Adding React and Typescript
Run the following commands to install React and Typescript:
1 | npm install react react-dom --save |
Add a new file called tsconfig.json in the folder with the following configuration:
1 | { |
Adding Webpack
Install webpack and related packages:
1 | npm install webpack webpack-cli webpack-dev-server --save-dev |
Next, add some commonly used loaders:
1 | npm install ts-loader css-loader source-map-loader --save-dev |
We need to add html-webpack-plugin as well in order to generate HTML files:
1 | npm install html-webpack-plugin --save-dev |
Now it’s time to configure webpack in our project. Create a file webpack.config.js in the root folder of our project:
1 | const path = require("path"); |
Adding index.html
Create src folder in the root directory and then add index.html in it:
1 |
|
Adding Components
Create App.tsx in src folder:
1 | import * as React from 'react'; |
Create index.tsx in the same folder:
1 | import * as React from 'react'; |
Adding npm scripts
Add new scripts in the scripts section in package.json file:
1 | "scripts": { |
Run Application
Now let’s run the script npm start, you will be able to visit your app at http://localhost:3000/.
Next Steps
Now that you have your React + TypeScript setup, you can start building your application! Consider adding state management, routing, and other features as you expand your project.
For more advanced Webpack configurations, check out the Webpack Documentation.