Setting up a React application manually using Webpack provides developers with fine-grained control over every aspect of the build process. While tools like Create React App (CRA) simplify the process, they abstract away the build configuration. For advanced scenarios—like optimizing bundle size, customizing Babel or PostCSS, or tweaking performance—setting up from scratch is invaluable.
In this article, we’ll walk through how to create a React app from the ground up using Webpack. You’ll learn how to bundle JavaScript, transpile JSX using Babel, handle styles, and set up hot module replacement.
Initialize the Project
Start by creating a new directory and initializing a Node.js project.
This will create a package.json
file where your project’s dependencies and scripts will be managed.
Install Webpack and Core Dependencies
Next, install Webpack and the necessary tools for bundling.
-
webpack
: Core bundler. -
webpack-cli
: Command-line interface for Webpack. -
webpack-dev-server
: Development server with live reloading.
Create the project structure:
Add a basic HTML skeleton in public/index.html
:
Add React and Babel for JSX Transpilation
Now install React and Babel to handle JSX and modern JavaScript features:
-
@babel/core
: Core Babel compiler. -
babel-loader
: Webpack loader for Babel. -
@babel/preset-env
: Enables modern JavaScript support. -
@babel/preset-react
: Supports JSX transpilation.
Create a Babel config file:
Add the following content:
Now update src/index.js
to render a simple React component:
Configure Webpack
Create the Webpack configuration file:
Here’s a basic configuration:
This sets up:
-
JavaScript/JSX transpilation.
-
Hot reloading via Webpack Dev Server.
-
React rendering on
localhost:3000
.
Add scripts to package.json
:
Add Style Handling (CSS, SASS, Modules)
Install style-related loaders:
Then update the Webpack config to include CSS support:
Add a CSS file to test:
Update your index.js
to import styles:
Optional: Add SASS Support
Add to your Webpack config:
You can now create styles.scss
and use variables or nesting.
Handle Images and Fonts
To support importing assets like images and fonts, install:
Add this to your Webpack config:
Now you can import images directly in your components:
Add ESLint and Prettier (Optional but Recommended)
Install linting tools:
Initialize ESLint:
Enable React rules and JSX support in .eslintrc
.
For Prettier:
Add to .eslintrc
:
Production Build Optimization
Update webpack.config.js
to support both development
and production
modes:
For better production optimization:
Add to your config:
Final Folder Structure
Conclusion
Manually setting up a React project with Webpack gives developers absolute control over the build process, module handling, and performance optimizations. While it involves more initial configuration than using tools like CRA, the payoff is a fully customized development environment.
Here’s what we’ve accomplished:
-
Initialized a React project with modern JavaScript and JSX support.
-
Configured Babel for transpilation.
-
Set up Webpack for bundling assets, styles, and media.
-
Added support for CSS and optional Sass.
-
Integrated ESLint and Prettier for code quality.
-
Distinguished between development and production environments.
-
Built a clean, scalable project structure.
Whether you’re building a minimal prototype or a large-scale SPA, understanding and mastering your build pipeline lays a strong foundation for performance, maintainability, and scalability.