How to Build MERN Stack for Production
A step by step guide with an example project
There are so many ways we can build React apps and ship them for production. One way is to build the React app with NodeJS and MongoDB as a database. There are four things that make this stack popular and you can write everything in Javascript. The four things are MongoDB, React, Express, and NodeJS. This stack can be used for a lot of uses cases in web development.
Developing the application is one part and you need to package it based on your deployment needs once the development part is completed. There are so many ways we can package and ship MERN Stack to production: manual, with webpack, with Gulp, etc we will see all these approaches in detail.
- Prerequisites
- Example Project
- MERN Stack Development
- Manual Implementation
- With Webpack
- Packaging With Gulp
- With Docker
- Summary
- Conclusion
Prerequisites
There are some prerequisites for this post. You need to have a NodeJS installed on your machine and some other tools that are required to complete this project.
- NodeJS
- Express Framework
- Mongoose
- MongoDB
- VSCode
- Postman
- nodemon
- dotenv
- Create React App
- Typescript
- react-bootstrap
- gulp.js
- Docker
NodeJS: As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.
Express Framework: Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
Mongoose: elegant MongoDB object modeling for node.js
MongoDB: MongoDB is a general-purpose, document-based, distributed database built for modern application developers and for the cloud era.
VSCode: The editor we are using for the project. It’s open-source and you can download it here.
Postman: Manual testing your APIs
nodemon: To speed up the development
If you are a complete beginner and don’t know how to build from scratch, I would recommend going through the below articles. We used these projects from this article as a basis for this post.
How to write production-ready Node.js Rest API — Javascript version
Example Project
Here is an example of a simple tasks application that creates, retrieves, edits, and deletes tasks. We actually run the API on the NodeJS server and you can use MongoDB to save all these tasks.
As you add users we are making an API call to the nodejs server to store them and get the same data from the server when we retrieve them. You can see network calls in the following video.
Here is a Github link to this project. You can clone it and run it on your machine.
// clone the project
git clone https://github.com/bbachi/mern-stack-example
// React Code
cd ui
npm install
npm start
// API code
cd api
npm install
npm run dev
MERN Stack Development
As we said earlier, MERN Stack uses four technologies such as MongoDB, Express, React, and NodeJS. React is a javascript library for building web apps and it doesn’t load itself in the browser. We need some kind of mechanism that loads the index.html (single page) of React application with all the dependencies(CSS and js files) in the browser. In this case, we are using node as the webserver which loads React assets and accepts any API calls from the React UI app.
Here is the complete detailed article about the development.
Manual Implementation
In this manual implementation, we build the React app and place the appropriate code into one folder and run or deploy the application. As a first step, we need to build the React app and all the static assets and built files are placed into the build folder.
// change to ui directory
cd ui
// build the app
npm run build
All the built and static assets are placed under this folder build/
Once you built the application, all you need to do is create a separate folder and place the nodejs related stuff in that folder. Let’s create a folder called mern-prod and put the api folder (node server code) and the folder build inside it.
Run the application
Let’s run the app by importing the whole folder mern-prod into the VSCode editor and installing the dependencies for the server.
//change the directory
cd mern-prod/api
// install dependencies
npm install
// run the app
node server.js
You need to make sure two things before you start the application. First, you need to put any environment variables such as MongoDB Connection string, PORT, etc. There are some other options as well check the below-detailed article.
// exporting env variables
export PORT=3080
export ENVIRONMENT=production
export MONGO_CONNECTION_STRING='mongodb+srv://tester:Tester@[email protected]/tasks?retryWrites=true&w=majority'
The second one is that update the UI static assets in the sever.js file as below. Notice the line numbers 20 and 41.