README.md 53.3 KB
Newer Older
1001
1002

### Building for Relative Paths
1003

Dan Abramov's avatar
Dan Abramov committed
1004
By default, Create React App produces a build assuming your app is hosted at the server root.<br>
Dan Abramov's avatar
Dan Abramov committed
1005
1006
1007
1008
1009
1010
1011
1012
To override this, specify the `homepage` in your `package.json`, for example:

```js
  "homepage": "http://mywebsite.com/relativepath",
```

This will let Create React App correctly infer the root path to use in the generated HTML file.

Dan Abramov's avatar
Dan Abramov committed
1013
### GitHub Pages
1014

1015
1016
>Note: this feature is available with `react-scripts@0.2.0` and higher.

Dan Abramov's avatar
Dan Abramov committed
1017
1018
#### Step 1: Add `homepage` to `package.json`

Alex Wilmer's avatar
Alex Wilmer committed
1019
1020
**The step below is important!**<br>
**If you skip it, your app will not deploy correctly.**
Dan Abramov's avatar
Dan Abramov committed
1021

Dan Abramov's avatar
Dan Abramov committed
1022
Open your `package.json` and add a `homepage` field:
1023
1024

```js
1025
  "homepage": "https://myusername.github.io/my-app",
1026
1027
```

1028
1029
Create React App uses the `homepage` field to determine the root URL in the built HTML file.

Dan Abramov's avatar
Dan Abramov committed
1030
#### Step 2: Install `gh-pages` and add `deploy` to `scripts` in `package.json`
Dan Abramov's avatar
Dan Abramov committed
1031

1032
Now, whenever you run `npm run build`, you will see a cheat sheet with instructions on how to deploy to GitHub Pages.
1033

1034
To publish it at [https://myusername.github.io/my-app](https://myusername.github.io/my-app), run:
1035
1036

```sh
1037
1038
1039
1040
1041
1042
1043
1044
1045
npm install --save-dev gh-pages
```

Add the following script in your `package.json`:

```js
  // ...
  "scripts": {
    // ...
1046
    "deploy": "npm run build&&gh-pages -d build"
1047
  }
1048
1049
```

1050
1051
(Note: the lack of whitespace is intentional.)

Dan Abramov's avatar
Dan Abramov committed
1052
1053
#### Step 3: Deploy the site by running `npm run deploy`

1054
1055
1056
1057
1058
Then run:

```sh
npm run deploy
```
1059

Dan Abramov's avatar
Dan Abramov committed
1060
1061
1062
1063
1064
1065
1066
#### Step 4: Ensure your project's settings use `gh-pages`

Finally, make sure **GitHub Pages** option in your GitHub project settings is set to use the `gh-pages` branch:

<img src="http://i.imgur.com/HUjEr9l.png" width="500" alt="gh-pages branch setting">

#### Step 5: Optionally, configure the domain
Dan Abramov's avatar
Dan Abramov committed
1067

1068
1069
You can configure a custom domain with GitHub Pages by adding a `CNAME` file to the `public/` folder.

Dan Abramov's avatar
Dan Abramov committed
1070
1071
1072
1073
#### Notes on client-side routing

GitHub Pages doesn't support routers that use the HTML5 `pushState` history API under the hood (for example, React Router using `browserHistory`). This is because when there is a fresh page load for a url like `http://user.github.io/todomvc/todos/42`, where `/todos/42` is a frontend route, the GitHub Pages server returns 404 because it knows nothing of `/todos/42`. If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:

1074
1075
1076
* You could switch from using HTML5 history API to routing with hashes. If you use React Router, you can switch to `hashHistory` for this effect, but the URL will be longer and more verbose (for example, `http://user.github.io/todomvc/#/todos/42?_k=yknaj`). [Read more](https://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md#histories) about different history implementations in React Router.
* Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your `index.html` page with a special redirect parameter. You would need to add a `404.html` file with the redirection code to the `build` folder before deploying your project, and you’ll need to add code handling the redirect parameter to `index.html`. You can find a detailed explanation of this technique [in this guide](https://github.com/rafrex/spa-github-pages).

1077
1078
### Heroku

Dan Abramov's avatar
Dan Abramov committed
1079
Use the [Heroku Buildpack for Create React App](https://github.com/mars/create-react-app-buildpack).<br>
1080
1081
1082
1083
1084
1085
You can find instructions in [Deploying React with Zero Configuration](https://blog.heroku.com/deploying-react-with-zero-configuration).

### Modulus

See the [Modulus blog post](http://blog.modulus.io/deploying-react-apps-on-modulus) on how to deploy your react app to Modulus.

1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
## Netlify

**To do a manual deploy to Netlify's CDN:**

```sh
npm install netlify-cli
netlify deploy
```

Choose `build` as the path to deploy.

**To setup continuous delivery:**

With this setup Netlify will build and deploy when you push to git or open a pull request:

1. [Start a new netlify project](https://app.netlify.com/signup)
2. Pick your Git hosting service and select your repository
3. Click `Build your site`

Ville Immonen's avatar
Ville Immonen committed
1105
**Support for client-side routing:**
1106
1107
1108
1109
1110
1111
1112
1113
1114

To support `pushState`, make sure to create a `public/_redirects` file with the following rewrite rules:

```
/*  /index.html  200
```

When you build the project, Create React App will place the `public` folder contents into the build output.

1115
1116
1117
1118
### Now

See [this example](https://github.com/xkawi/create-react-app-now) for a zero-configuration single-command deployment with [now](https://zeit.co/now).

1119
1120
1121
1122
### S3 and CloudFront

See this [blog post](https://medium.com/@omgwtfmarc/deploying-create-react-app-to-s3-or-cloudfront-48dae4ce0af) on how to deploy your React app to Amazon Web Services [S3](https://aws.amazon.com/s3) and [CloudFront](https://aws.amazon.com/cloudfront/).

1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
### Surge

Install the Surge CLI if you haven't already by running `npm install -g surge`. Run the `surge` command and log in you or create a new account. You just need to specify the *build* folder and your custom domain, and you are done.

```sh
              email: email@domain.com
           password: ********
       project path: /path/to/project/build
               size: 7 files, 1.8 MB
             domain: create-react-app.surge.sh
             upload: [====================] 100%, eta: 0.0s
   propagate on CDN: [====================] 100%
               plan: Free
              users: email@domain.com
         IP Address: X.X.X.X

    Success! Project is published and running at create-react-app.surge.sh
```

Brian Ng's avatar
Brian Ng committed
1142
Note that in order to support routers that use HTML5 `pushState` API, you may want to rename the `index.html` in your build folder to `200.html` before deploying to Surge. This [ensures that every URL falls back to that file](https://surge.sh/help/adding-a-200-page-for-client-side-routing).
1143

1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
## Troubleshooting

### `npm test` hangs on macOS Sierra

If you run `npm test` and the console gets stuck after printing `react-scripts test --env=jsdom` to the console there might be a problem with your [Watchman](https://facebook.github.io/watchman/) installation as described in [facebookincubator/create-react-app#713](https://github.com/facebookincubator/create-react-app/issues/713).

We recommend deleting `node_modules` in your project and running `npm install` (or `yarn` if you use it) first. If it doesn't help, you can try one of the numerous workarounds mentioned in these issues:

* [facebook/jest#1767](https://github.com/facebook/jest/issues/1767)
* [facebook/watchman#358](https://github.com/facebook/watchman/issues/358)
* [ember-cli/ember-cli#6259](https://github.com/ember-cli/ember-cli/issues/6259)

It is reported that installing Watchman 4.7.0 or newer fixes the issue. If you use [Homebrew](http://brew.sh/), you can run these commands to update it:

```
watchman shutdown-server
brew update
brew reinstall watchman
```

You can find [other installation methods](https://facebook.github.io/watchman/docs/install.html#build-install) on the Watchman documentation page.

If this still doesn't help, try running `launchctl unload -F ~/Library/LaunchAgents/com.github.facebook.watchman.plist`.

There are also reports that *uninstalling* Watchman fixes the issue. So if nothing else helps, remove it from your system and try again.

1170
1171
1172
1173
### `npm run build` silently fails

It is reported that `npm run build` can fail on machines with no swap space, which is common in cloud environments. If [the symptoms are matching](https://github.com/facebookincubator/create-react-app/issues/1133#issuecomment-264612171), consider adding some swap space to the machine you’re building on, or build the project locally.

Dan Abramov's avatar
Dan Abramov committed
1174
## Something Missing?
Dan Abramov's avatar
Dan Abramov committed
1175

Ville Immonen's avatar
Ville Immonen committed
1176
If you have ideas for more “How To” recipes that should be on this page, [let us know](https://github.com/facebookincubator/create-react-app/issues) or [contribute some!](https://github.com/facebookincubator/create-react-app/edit/master/packages/react-scripts/template/README.md)
For faster browsing, not all history is shown. View entire blame