Setup React + Laravel without laravel mix
Tags: react, laravel, webpack
Published: 2019-04-12 19:31:39

---
title: Setup React + Laravel without laravel mix
published: true
description: Setting up a project with React(FE) and Laravel(BE) without using laravel mix
tags: react, laravel, webpack
---

![react+laravel](https://pbs.twimg.com/media/CCKEzPaWgAADyDK.png)
When it comes to web development, my goto library on the front end is [React](https://reactjs.org), and on the backend, I love to use [Laravel](https://laravel.com) because of the many built-in goodies it contains.

More than once i have found my self having to host my backend and my frontend separately, and running multiple deployments. Although it has its benefit, it can be quite expensive to maintain. Deploying your react and laravel app on the same server still gives you most of the benefits of deploying them separately. They might live on the same server but they are still two reusable entities communicating with each other. Let me show how you can set up your laravel and react project on one server.

For this task, we are going to be making use of [Babel](https://babeljs.io), [webpack](webpack
https://webpack.js.org) and [react-hot-loader](https://github.com/gaearon/react-hot-loader) to compile our react assets into the laravel view. 

## Laravel
First, we are going to create our laravel project. We can do that by running the command.
``` laravel new react-laravel ```
You can learn more about creating a laravel project [here](https://laravel.com/docs/5.8/installation). 

In our `/resources/view/` folder, we can find a file named `welcome.blade.php`. Delete the file and create a new file called `index.blade.php` with the following content.

{% gist https://gist.github.com/codenaz/51faf0183df1bf8f8f235e5eb0e52b20 %}

Line 16 checks if we are running in development mode so that it can fetch the compiled asset from the webpack dev server. It's important that you update your `APP_ENV` in the env file to 'production' when in a production environment.

Then we have to modify our route to point to that file. So will head into our routes folder and open `web.php`. We will replace 'welcome' with 'index' and our file should end up looking like this:

{% gist https://gist.github.com/codenaz/38a76f957994625519d2c99dbd54c9e1 %}

## package.json
Now it's time to modify our `package.json` file which can be found in the root of the project. A number of modules will not be needed, so we should modify the file to look like this.

{% gist https://gist.github.com/codenaz/5c38e0814e816b86d1c124164ef34739 %}

We will install all we need along the line.

## React
Now we want to add react to our laravel project. Create a folder named 'src' in the root of our laravel project. Next, let's get Babel

### Babel
To install babel, let's run ```npm install --save-dev @babel/core@7.1.0 @babel/cli@7.1.0 @babel/preset-env@7.1.0 @babel/preset-react@7.0.0```

We won't go into details of what each of this packages do so that this article doesn't get too long, but I will advise that you do your little research if you are not yet familiar with them.

Create a file named `.babelrc` in the project root. We will be setting the presets for babel by inputting this content in the file.

{% gist https://gist.github.com/codenaz/063fb59a9c31cf4738256706b00ebfb3 %}

### Webpack
Now we need to get and configure webpack. To do that we will need to install a few more packages. Let's run ``` npm install --save-dev webpack@4.19.1 webpack-cli@3.1.1 webpack-dev-server@3.1.8 style-loader@0.23.0 css-loader@1.0.0 babel-loader@8.0.2 ```.

Webpack uses loaders to process different types of files. it also comes with a development server that will use to bundle and serve our React project during development. You can do some research on webpack if you are not already familiar with it.

Create a new file named `webpack.config.js` also at the root of the laravel project. This file exports an object which will be our webpack configuration.

{% gist https://gist.github.com/codenaz/ae323388ecbb703cbaa64553bc3fac18 %}

This configuration tells webpack what file to start bundling from (entry), the type of files that are transformed(module), files to leave out(exclude), and where it saves the bundled file to (output).

Let's go ahead and install a loader for processing sass files since it’s part of the modules we’ve defined in our config.
``` npm install sass sass-loader```

You can add more loaders depending on your needs.

### React

Next, we'll add two more packages by running ```npm install --save react@16.18.4 react-dom@16.8.4```. Notice we are installing these as regular dependencies.

We can now create our `index.js` file in the `src` directory. This is the file that tells react when to hook into the dom.

{% gist https://gist.github.com/codenaz/2a3f397a6472916279f406bb9a3ca172 %}

We can also create another file in `src` called `App.js`. All these should be familiar if you've previously worked with react.

{% gist https://gist.github.com/codenaz/76c9256fd950235c064da2001f0b941d %}

From our webpack config, our app can also process css, so will create and add a css file. Create a folder named `style` in our `src` directory and in the `style` directory create a filed named `App.css`. We will add some style to this file

{% gist https://gist.github.com/codenaz/95ebfb7bffdeadcfb1cb1d7e48a233bc %}.

Your final project struture should be similar to this:

```
.
+-- app
+-- bootstrap
+-- config
+-- database
+-- node_modules
+-- public
| +-- .htacess
| +-- favicon.ico
| +-- index.html
| +-- index.php
+-- resources
| +-- views
| | +-- index.blade.php
+-- routes
+-- src
| +-- style
| | +-- App.css
| +-- App.js
| +-- index.js
+-- storage
+-- test
+-- vendor
+-- .env
+-- .env.example
+-- .babelrc
+-- .gitignore
+-- artisan
+-- composer.json
+-- composer.lock
+-- package-lock.json
+-- package.json
+-- server.php
+-- webpack.config.js
```

We now have a function React & Laravel app! We can start up our dev server to compile our assets by running ```npm start``` and the start our php server to run the laravel app by running ```php artisan serve``` in the terminal. We should see our app on `http://localhost:8000`

## HMR
If you run the server now, you will notice none of our changes on react updates the app. That is because HMR doesn't know what to replace yet. 

We are going to complete our setup by installing `react-hot-loader`. So run ```npm install --save react-hot-loader```.

Now import react-hot-loader in our app and wrap it around the exported component. Your App.js should now look like this

{% gist https://gist.github.com/codenaz/9ea516e93ea379709fbac47489199fd1 %}.

Now our app will be updated as we make changes to the react app. Changing our PHP files will not cause the app to update, just the js files in the `src` folder.

When your app is ready for deployment, run ```npm run build``` to build our asset and update the APP_ENV in our env file to 'production' so that laravel will fetch the built asset and not the compiled asset in the dev server.

## Conclusion
You can go ahead an tweak the setup according to your need. If anything is still unclear or you want another reference, [here](https://github.com/codenaz/reavel) is a repo with the implementation on Github.