How to Host ReactJS Websites on Vercel with Environment Variables ?
Last Updated :
02 Nov, 2023
In this article, we will learn how to host our ReactJs projects on Vercel and use Environment Variables in this tutorial. Vercel is a platform for developers to deploy and host their projects online, where they can showcase their skills to others.
What is an Environment Variable?
We conceal API keys, database linkages, and other crucial information from the general public using environment variables. Additionally, it ensures that users have access to very important information on our production-level website or project and helps us protect our database from hackers.
Also, if you share your codes on any public repository like GitHub then you don't want to share your API keys, because if it is a paid service then every API request made through your API key will cost you a dollar. So, it is important to hide this information from the public repository as well.
What is Vercel?
Vercel is a platform for developers to deploy and host their projects online, where they can showcase their skills to others. It is really easy to use, as developers can easily link their Github profiles and easily deploy their projects. Additionally, this platform gives developers access to numerous premium features without charge.
Prerequisites:
Steps to create the project:
Step 1: Command to create Reactjs project in the terminal.
npx create-react-app project-name
Step 2: Now we have to install some npm packages to run our project smoothly:
npm i dotenv
The Project Structure will look like:
File Structure
Include the name of the ".env" file in the ".gitignore" file after this.
Step 3: Code for App.js and .env files:
We will first declare the project's environmental variables in the ".env" file. Prior to that, always label your variables "REACT_APP"; this is how development nomenclature works.
.env file:
REACT_APP_NAME = SK ELAF AHMED
REACT_APP_COMPANY = GEEKS FOR GEEKS
REACT_APP_HOSTING = VERCEL
Example: Below is the basic implementation of the above steps
JavaScript
import './App.css';
function App() {
return (
<div className="App">
<h1>
Hi, I am {process.env.REACT_APP_NAME}
</h1>
<h1>
I work in {process.env.REACT_APP_COMPANY}
</h1>
<h2>
Here we are hosting this website on
{process.env.REACT_APP_HOSTING}
</h2>
</div>
);
}
export default App;
Explanation: After declaring the variables, we wrote the code in our App.js file, and to access the variables we have to write them as:
process.env.VariableName
Output on localhost:
LocalHostStep 4: Create a GitHub repository and push the local code to the remote repository:
You can set up a GitHub repository, and then we'll merely carry out the subsequent actions. To push the code to GitHub, we must perform a few commands on the terminal of our local projects.
git init
git add .
git commit -m "First Commit"
git remote add origin "Repository Link"
git push origin master
Now, after successfully uploading the code the repository will look like this:
Repository for this projectStep 5: Now we will open Vercel and host this website:
Visit Vercel.com, and if you are new then we suggest you sign up with your GitHub account because eventually, we have to connect the GitHub repository with Vercel. After that, you will see the dashboard then click on "Add New" > "Project" as shown in the image:
Vercel Dashboard
Now, if you already connected your GitHub account then the repositories will be shown there, and select the repository for your project.
Configure Your Project
We must declare them on the Environment Variables tab, as we can see above. Additionally, Vercel always encrypts them to ensure customer privacy is never compromised. Don't forget to include each Environment variable one at a time.
After that, hit the "Deploy" button and wait since constructing the react project requires some time. Once your website has been properly launched, you will be given a deployment URL that will allow you to view it and share it with others.
Dashboard
This is the dashboard following a successful deployment. If you click the link, your host's website with the domain Vercel will open.
Output:
Hosted Website with Vercel Domain
The website URL is successfully hosted on Vercel with the appropriate Environment Variables.
Similar Reads
How to Use Environment Variables in Vite?
In order to create your Vite application in different environments such as development, staging and production, you need to use environment variables. They enable you to manage confidential data like API keys or service URLs without having to code them inside your source code. By using environment v
3 min read
How to Load environment variables from .env file using Vite?
The environment variables in the application are managed through .env files in a Vite project, allowing you to configure and access various settings dynamically. By prefixing variables with VITE_, Vite exposes them to your applicationâs runtime environment. This approach facilitates different config
2 min read
Environment Protection Website using React
Imagine building a website that champions environmental protection. That's exactly what we're aiming for with this project. We're crafting a space using React where people can learn, share, and engage in all things related to safeguarding our planet. Output Preview: Let us have a look at how the fin
7 min read
How To Configure And Use Environment Variables in NestJS?
Environment variables are an important part of application development, allowing developers to configure applications in different environments (development, staging, production) without hardcoding sensitive or environment-specific information into the application code. In this article, we'll walk t
2 min read
Next.js Environment Variables
In this article, we are going to see how to use environment variables in Next.js. Environment variables in Next.js are a way to set configuration values that are used by your application. They can be used to store data such as the name of your company, the port your application will run on, or any o
2 min read
How To Create a Website in ReactJS?
ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS. PrerequisitesNPM & Node.js
5 min read
How Can I Use Vite Env Variables in vite.config.js?
Vite is a fast and modern front-end build tool that provides an efficient way to develop web applications. One of its powerful features is the ability to use environment variables, which can help you customize your app's behaviour based on different environments. This guide will walk you through how
4 min read
Reading Environment Variables From Node.js
Environment Variable: The two fundamental concepts of any programming language are variables and constants. As we know that constants and variables both represent the unique memory locations that contain data the program uses in its calculations. The variable which exists outside your code is a part
2 min read
How to Host HTML, CSS & JavaScript Website on Vercel ?
In this article, we will host HTML, CSS & JavaScript Websites on Vercel. Every web developer wants to not only create a website but also host it properly, ensuring that visitors and others have a problem-free experience while visiting your website and with Vercel you can seamlessly host your web
4 min read
How to use React or Vue with Vite and Docker?
This guide will provide details on how to create a Docker container with a new React project integrated with Vite. When combined, with Viteâs short build times and Dockerâs ability to create a consistent and reproducible environment, you can quickly achieve developmental parity with the production e
3 min read