Setup on a Local Machine
Discover how to prepare your local machine for React development by installing Node and npm, setting up a React project with create-react-app, and running the app efficiently in Visual Studio Code. This lesson equips you with essential tools and commands to start building React applications.
Requirements
The following are requirements for local setup:
Editor and Terminal
I have provided a setup guide to get you started with general web development. For this learning experience, you will need a text editor (e.g., Sublime Text) and a command-line tool (e.g., iTerm) or an IDE (e.g. Visual Studio Code). I recommend Visual Studio Code (also called VS Code) for beginners, as it’s an all-in-one solution that provides an advanced editor with an integrated command-line tool, and because it’s a popular choice among web developers.
Throughout this learning experience I will use the term command-line, which will be used synonymously for the terms command-line tool, terminal, and integrated terminal. The same applies to the terms editor, text editor, and IDE, depending on what you decided to use for your setup.
Optionally, I recommend managing projects in GitHub while we conduct the exercises in this course, and I’ve provided a short guide on how to use these tools. Github has excellent version control, so you can see what changes were made if you make a mistake or just want a more direct way to follow along. It’s also a great way to share your code later with other people.
Because it is so popular, we will specifically look at how to run a React app in VS Code using its built-in features later in this section.
Node and NPM
Before we can begin, we’ll need to have node and npm installed. Both are used to manage libraries (node packages) you will need along the way. These node packages can be libraries or whole frameworks. We’ll install external node packages via npm (node package manager).
If you have already installed Node and npm, make sure that your installation is the most recent version. If you’re new to npm or need a refresher, this npm crash course I created will get you up to speed.
📜 You can verify node and npm versions in the command line using the
node --versioncommand.
How to install React
After installing Node and npm, you are ready to learn how to install react using the create-react-app tool. Use your command line to type the following command in a dedicated folder for your project. We will refer to this project as hacker-stories, but you may choose any name you like:
npx create-react-app hacker-stories
Navigate into your new folder after the setup has finished:
cd hacker-stories
How to run a React app in VS Code
Now, we can open the application in an editor. To run a react app in vs code, the process is very straightforward because of the integrated terminal.
- Open VS Code.
- Open your project folder (
hacker-stories). - Alternatively, if you have the command-line tools installed, just type
code .in your terminal within the project folder.
The following folder structure, or a variation of it depending on the create-react-app version, should be presented:
hacker-stories/
--node_modules/
--public/
--src/
----App.css
----App.js
----App.test.js
----index.css
----index.js
----logo.svg
--.gitignore
--package-lock.json
--package.json
--README.md
How to run React project
After you have learned about the folder and file structure of your React project, let’s go through the available commands to get it started. All your project-specific commands can be found in your package.json under the scripts property. They may look similar to these:
{
...
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
...
}
These scripts are executed with the npm run <script> command in an IDE-integrated terminal or command line tool. The run can be omitted for the start and test scripts. The commands are as follows:
Runs the application in http://localhost:3000
npm start
Runs the tests
npm test
Builds the application for production
npm run build
📜 Another command from the previous npm scripts called
ejectshouldn’t be used for this learning experience. It’s a one-way operation. Once you eject, you can’t go back. Essentially this command is only there to make all the build tool and configuration from create-react-app accessible if you are not satisfied with the choices or if you want to change something. Here we will keep all the defaults though.