Step 1: Installing Surge Globally
Before deploying your React app, you'll need to install the Surge CLI (command-line interface) globally on your system.
This allows you to use the surge
command from any directory in your terminal.
Open your VS Code terminal and run the following command:
npm install -g surge
Note: You need Node.js and npm (Node Package Manager) installed on your system for this command to work. If you are running this command for the very first time you will be asked to create an account. Follow the log in procedure and the same will apply from here on.
Step 2: Building Your React Application
Before deploying, you need to create a production build of your React application.
This process optimizes your code for deployment, bundles all necessary files, and creates a build
folder containing the deployable assets.
In your VS Code terminal, navigate to your React project directory and run the following command:
npm run build
This command uses the build script defined in your package.json
file to generate the production-ready build. Once the build process is complete, a build
folder will be created in your project directory.
Step 3: Deploying with Surge
Now that you have a production build, you can deploy your React application using Surge.
This step involves navigating to your project directory in the VS Code terminal and running the surge
command.
Open your VS Code terminal and run the following command:
surge
Surge will then Prompt you to specify the project path. Enter build
(or the name of your build directory) and press Enter. Surge will display a default domain. If you wish to change the domain name enter you domain name and press enter.
Once you have typed in the directory path and the domain, Surge will begin uploading your project. Once the upload is complete, Surge will provide a success message along with your deployment URL.