React

Creating & Running React Projects With create-react-app

The 'create-react-app' CLI tool allows you to quickly create & run React applications with no configuration

Valeri Karpov Up to date (Dec '17) React
PRO

Starting new React projects used to be a huge hassle -- there were dozens of dependencies, configuration files, and other up front requirements before you could even start writing a single line of React code.

Enter create-react-app, the tool recently released by Facebook's engineering team that allows you to spin up a brand new React application in just a few seconds. They do this by wrapping all of the normal dependencies (babel, etc) so you can just focus on writing React code itself. It's an excellent piece of software and is already becoming the de facto way to manage React projects.

Enough talking about it though, lets get started with it!

Install it on your machine

Install create-react-app from npm
npm install -g create-react-app

You'll want to install it globally (hence the -g flag)

Creating a new React app

It's really simple to create a new app -- simply run create-react-app followed by the desired name of your application, and it will scaffold a new app for you. Lets try it out:

Create a new app called "example-app"
create-react-app example-app
cd example-app

The folder example-app/ was created by create-react-app and houses all of our new application's code.

The project layout should look like this:

example-app/
  README.md
  node_modules/
  package.json
  .gitignore
  public/
    favicon.ico
    index.html
  src/
    App.css
    App.js
    App.test.js
    index.css
    index.js
    logo.svg

Notice the lack of complex folder/file structures, build configurations, etc -- just the essential files that you need to build your app.

Building & running an application

Inside of your application's folder, you can run the following commands (listed here from the create-react-app repo)

npm start

Runs the app in development mode. Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits. You will see the build errors and lint warnings in the console.

npm test

Runs the test watcher in an interactive mode.
By default, runs tests related to files changes since the last commit.

Read more about testing.

npm run build

Builds the app for production to the build folder. It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes. Your app is ready to be deployed!

Real World usage

The React + Redux RealWorld codebase uses create-react-app. Simply cloning the repo and running npm install will get everything set up, and running npm start will let you run the application live.

In the next tutorial, we'll integrate Redux into an app that's scaffolded by create-react-app!

 

I finished! On to the next tutorial