The Efficient Way to Create a MERN App
· 228
Wajdi Alkayal Wajdi Alkayal

Whether you are a newbie or an expert in the programming space, you‘ve probably wondered how others developers set up and configure their starting project application.

This article tackles an efficient and intelligent way to set up a production-ready application. We will look into how to set up a MERN Stack application with tools, frameworks and modules that will allow us to manage the performance, styling, asynchrony, and other essential stuff we need to build a real-world application.

Project Structure

First, create a new folder called mern-app-setup. Enter this folder and initialize our full stack application by entering and running this command in your terminal the command:

You will be asked a series of questions to gather meta-information about our project such as name, author and license. Then a package.json file will be auto generated with your answers and meta-information about the application as well as list of modules dependencies.

Alternatively, you can also initialize it using yarn init if you are using yarn package manager. Before running theses commands make sure that you have already installed the latest Node.js , npm or yarn on your local machine. Click on each of those links to know more about the process.

Now, let us install all the necessary dependencies and devDependencies that we need. Open package.json file and modify the JSON object to add the key modules and dependencies.

Dependencies:

DevDependencies: Modify package.json further to add the following Node modules required during development as devDependencies:

Save the package.json and run npm install from the command line to fetch and add all these modules to the project. With all the necessary modules installed and added to the project, next we will add configuration to compile and run a simple application.

Note that you can also install all these modules manually on the command line and work with the latest versions modules dependencies but we use this method just in case you will be reading this article a year or two years from now, so you will have the same code base and modules versions here in case you want to follow along with us.

Configuring Babel, Webpack and Nodemon

Now, we will configure our application with Babel, Webpack and Nodemon to compile, bundle and auto-reload the changes in the code during the development. Before diving to these configurations, let us learn a few things about each module.

Babel is a JavaScript transcompiler that converts edge JavaScript into plain old ES5 JavaScript that can run in any browser (even the old ones). It makes available all the syntactical sugar that was added to JavaScript with the new ES6 specification, including classes, fat arrows and multiline strings.

Webpack is a JavaScript code bundler that traverses the dependency graph of your project (chain of imports you use in your JS files), and creates a static JavaScript file(s) that is ready to be attached to your HTML.

Create a file named .babelrc in the root directory of the project and add the following JSON with presets and plugins specified:

mern-app-setup/.babelrc

In this configuration, we specify that we need Babel to transpile the latest JavaScript syntax with support for code in a Node.js environment and also React/JSX code. The react-hot-loader/babel plugin is required by the react-hot-loader module to compile React components.

First, we will have to configure Webpack to bundle both the client and server code and the client code separately for production.

Create webpack.config.client.js, webpack.config.server.js, and webpack.config.client.production.js files in the project folder.

All three files will have the following code structure, starting with imports, then the definition of the config object, and finally the export of the defined config object:

The config JSON object will differ with value specific to client or server-side code, and development versus production code.

In the next sections, we will underline the relevant properties in each configuration instance.

Now, let us explain each property and method added in this file:

  • The name property defines which kind of environment our code will run in. Here we are setting up webpack to bundle the client-side code, so we need to specify browser.
  • The mode sets the node environment to the given value and tells webpack to use its built-in optimizations accordingly. If not set explicitly it defaults to a value of “production”.
  • The Devtool specifies how source maps are generated, if at all. Generally, a source map provides a way of mapping code within a compressed file back to its original position in a source file to aid debugging.
  • The entry point indicates which modules webpack should use to start bundling, in this case with the main.js in the client folder.
  • The output property tells webpack where to emit the bundles it creates and how to name these files. In this case, it sets to dist/bundle.js.
  • The module property allow webpack to process other types of files and convert them into valid modules that can be consumed by our application and added to the dependency graph.
  • At high level , Loaders have two properties: test and use. The test property identifies which file or files should be transformed and the use indicates which loader should be used to do the transforming. In our case, we choose to transpile all files with .jsx extensions , and the transpilation tool that we used is babel-loader.
  • HotModuleReplacementPlugin enables hot module replacement for react-hot-loader.
  • NoEmitOnErrorsPlugin allows skipping emitting when there are compile errors.

We also add a Webpack alias to point react-dom references to the @hot-loader/react-dom version.

Now, our client-side configuration code is ready to be loaded in the browser from the bundled code in bundle.js. In the next section, we will configure webpack for production.

In order to bundle the React code to be used in production mode, we need to set up webpack for production by updating the config object with following code:

mern-app-setup/webpack.config.client.production.js

The configuration here is similar to the client-side configuration for development mode, but without the hot-reloading plugin and debug configuration as these will not be required in production.

In order to configure webpack for server-side, update the webpack.config.server.js by adding the following code:

mern-app-setup/webpack.config.server.js

The code inside this file has the same structure as the webpack.config.client.js. But here we have not defined the mode option and add some additional properties like: libraryTarget and externals.

Webpack starts bundling from the server folder with server.js, then outputs the bundled code in server.generated.js in the dist folder. Now, with the bundling configurations in place, we can add configuration for running these generated bundles automatically on code updates during development using Nodemon.

Create a file called nodemon.json in the folder project and add the following configuration:

mern-app-setup/nodemon.json

This configuration will set up nodemon to watch automatically for changes in the server files during development, then execute compile and build commands as necessary.

Now, we have finished to configure Webpack and Babel. Let’s verify if the configuration is well done by building a simple full stack application.

In order to start building the frontend of our application, let’s create a root template file called template.js in the root directory of our project, which will render the HTML with React components.

Add the following code in this file: mern-app-setup/template.js:

When the server receives a request to the root URL, this HTML template will be rendered in the browser, and the div element with ID “root” will contain our React component. Next, create a folder called client . Inside this folder, create two files: index.js and App.js.

The index.js file simply renders the top-level entry React component in the div element in the HTML document:

mern-app-setup/client/index.js

In this case, the entry React component is the App component imported from App.js:

mern-app-setup/client/App.js

The App.js file contains a basic App React component, which is hot-exported to enable hot reloading with react-hot-loader during the development.

In order to see the React component rendered in the browser when the server receives a request to the root URL, we need to use the webpack and Babel setup to compile and bundle this code, and also add server-side code that responds to the root route request with the bundled code. We will implement this in the server-side next.

In the project folder, create a new folder called server. Inside this folder two files called: server.js, devBundle.js. The server.js file will set up the server and the devBundle.js file will help to compile the React code using the webpack configuration while in the development mode.

Now, let us implement the Node-express app which initiates client-side code bundling, starts the server, sets up the path to serve static assets to the client, and renders the React view in a template when a GET request is made to the root route.

In order to initialize our Express app, we will first import the Express module and then use this Express app to build the rest of the application. The resume code looks like this:

mern-app-setup/server/server.js

Next, we will initialize webpack to compile the client-side code when the server is running. In devBundle.js, we will set up a compile method that takes the Express app and configures it to use the Webpack middleware to compile, bundle, and serve code, as well as enable hot reloading in development mode:

mern-app-setup/server/devBundle.js

We have imported another module called dotenv in order to load our environment variables from a .env file into process.env. Install it by running npm install dotenv from the command line.

Now, let us call this compile method in server.js by adding the following lines while in the development mode:

mern-app-setup/server/server.js

Webpack will compile client-side code in both development and production mode, then place the bundled files in the dist folder. In order to make these static files available on requests from the client side, we will update our server.js file to serve static files from the dist folder:

mern-app-setup/server/server.js

When the server receives a request at the root URL / , we will render template.js in the browser. In order to receive GET requests as ‘/’, go to server.js and add the following route-handling code to the Express app:

Now let us finalize the server-side app by configuring the Express app to start a server that listens on the specified port for incoming requests:

With this code, when the server is running, it will be able to accept requests at the root route and render the React view with the “Hello! We are setting up a real-world application!” text in the browser.

We have finished to configure our MERN Stack application. Now we need to add some scripts in the package.json file so that we can run the application.

mern-app-setup/package.json

To run the code developed so far, and to ensure that everything is working, you can go through the following steps:

  • Run the application from the command line using npm run development to get Nodemon , Webpack and server started for development. After, open the root URL in the browser which is http://localhost:3000 if you are using your local machine. You should see a page with the text: “Hello! We are setting up a real world application!”
  • Run the application from the command line using npm run build to generate the client and server code bundles for production mode.
  • Run the application from the command line using npm start to run the bundled code in production.

We have finished setting up our MERN application with Webpack , Babel and Nodemon. But we have not implemented a way to improve our code quality and avoid errors. You can make it by using Prettier and Eslint. Read this articles to have an idea about the process to do this.


Related Posts
Graphic design
09 June
The Power of Email Marketing
03 June
Photography
01 June

WMK Tech Copyright © 2024. All rights reserved