168

Facebook provides a create-react-app command to build react apps. When we run npm run build, we see output in /build folder.

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!

How can we use custom folder instead of /build for the output? Thanks.

6324
  • 4,678
  • 8
  • 34
  • 63
  • see this [question](https://stackoverflow.com/questions/44448851/how-do-i-change-src-folder-to-something-else-in-create-react-app/48590867#48590867) and try change 'build' to 'whatever'. Haven't tried it. – nikrb Feb 03 '18 at 15:52
  • 3
    this is officially supported now, scroll down to https://stackoverflow.com/a/62378644/214446 – mb21 Apr 10 '21 at 13:39

18 Answers18

176

With react-scripts >= 4.0.2, this is officially supported:

By default, Create React App will output compiled assets to a /build directory adjacent to /src. You may use this variable to specify a new path for Create React App to output assets. BUILD_PATH should be specified as a path relative to the root of your project.

// package.json
  "scripts": {
    "build": "BUILD_PATH='./dist' react-scripts build",
    // ...
  },

or adding a .env file to the root of your project:

# .env
BUILD_PATH='./dist'

Caution: the path specified in BUILD_PATH will be wiped out without mercy. Double check that your environment variable is specified correctly, especially when using continuous integration.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
Andrew H.
  • 1,769
  • 2
  • 8
  • 3
  • 5
    I hope it gets in! This is super annoying that they refuse to support it. – Will Farley Nov 11 '20 at 03:50
  • 3
    This configuration was published today with version 4.0.2, it's on the advanced configuration docs and the changelog. – Ahmed Mohamedeen Feb 03 '21 at 19:53
  • 3
    This solution is great! **Important warning** When writing a BUILD_PATH make sure that the path you write doesn't exist or you don't mind be erased. Doing `BUILD_PATH='..'` made some serious damage to progress I made on my project. (It deleted git as well.) – barshopen Apr 25 '21 at 15:10
  • 12
    For windows, you need to use set. For example: `"build": "set BUILD_PATH=../wwwroot/clientapp && react-scripts build"` – Dan Apr 25 '22 at 06:47
  • This way is the best when used on both Windows & Mac environments – Tristanisginger May 23 '22 at 15:31
  • To expand on Dan's answer: On Windows you need to use "set" before BUILD_PATH. You can NOT use '' around the path. You also have to remember the && before react-scripts. – Bjarte Aune Olsen Jun 10 '22 at 07:04
  • "'BUILD_PATH' is not recognized as an internal or external command,"?? – user2301515 Mar 14 '23 at 09:49
  • Warning! You can inadvertently create folders with a space at the end which can be difficult to delete in Windows. Escape the quotes: `"build": "set \"BUILD_PATH=../my_path/to_somewhere\" && react-scripts build"` If you are having difficulty deleting the folder you can use cmd: `rd /s "\\?\C:\project\my_path\to_somewhere "` – sybb Jun 09 '23 at 13:50
141

Edit your package.json:

"build": "react-scripts build && mv build webapp"
Félix
  • 1,685
  • 2
  • 11
  • 6
  • 47
    I had to make it `"build": "react-scripts build && rm -rf docs && mv build docs"` otherwise after the first run it moves the build _into_ the docs folder instead of overwriting it. – TimoSolo Feb 01 '18 at 10:16
  • 3
    The commands equivalent for Windows ``rm -rf webapp = DEL /S /Q webapp`` and ``mv build webapp = move build webapp`` – Taha Farooqui Oct 02 '20 at 11:13
  • 2
    If you don't want to move build folder, just copy its files. commands equivalent for Windows : ``mv build webapp = xcopy /E /H /C /I build webapp`` – Taha Farooqui Oct 02 '20 at 12:09
  • 2
    If you want to move have things inside build on webapp and not webapp/build, the commands for Windows are `"build": "react-scripts build && RMDIR /S /Q webapp && move build webapp"` – Sergio Gutiérrez Oct 06 '20 at 11:15
  • 1
    To make it run across platforms (including Windows), first install an npm package called [cross-env](https://www.npmjs.com/package/cross-env) using `npm i --save-dev cross-env`. And then, in your package.json: `"build": "cross-env react-scripts build && mv build webapp"` – Nikhil Sinha Oct 15 '20 at 22:59
  • Not good for wanting to build for a new environment without touching your current local `build` directory. This still clobbers that directory. – GoForth Apr 19 '22 at 17:03
  • For newer versions of react-scripts, see [Andrew H's answer](https://stackoverflow.com/a/62378644/419294) – Dan Apr 25 '22 at 06:52
76

Create-react-app Version 2+ answer

For recent (> v2) versions of create-react-app (and possible older as well), add the following line to your package.json, then rebuild.

"homepage": "./"

You should now see the build/index.html will have relative links ./static/... instead of links to the server root: /static/....

Malcolm Dwyer
  • 1,145
  • 8
  • 9
  • 5
    Thanks. This should be the accepted answer. VS Code will warn you if you don't use an absolute URL, but the build works with relative URLs as well. – Hari May 14 '19 at 22:16
65

Edit: Support for a configurable BUILD_PATH just landed into v4.0.2. See t_dom93's answer.

You can't change the build output folder name with the current configuration options.

Moreover, you shouldn't. This is a part of the philosophy behind create-react-app: they say Convention over Configuration.

If you really need to rename your folder, I see two options:

  1. Right after the build process finishes, write a command that copies the build folder content to another folder you want. For example you can try the copyfiles npm package, or anything similar.

  2. You could try to eject create-react-app and tweak the configuration.

If you aren’t satisfied with the build tool and configuration choices, you can eject at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

However, it is important to note that this is a one-way operation. Once you eject, you can’t go back! You loose all future updates.

Therefore, I'd recommend you to not use a custom folder naming, if possible. Try to stick with the default naming. If not an option, try #1. If it still doesn't work for your specific use-case and you're really out of options - explore #2. Good luck!

Kaloyan Kosev
  • 12,483
  • 8
  • 59
  • 90
  • 3
    what advantage would copying the files have over renaming the folder? – azium Jan 06 '17 at 02:20
  • my main concern was that it might cause troubles if any of the other `react-create-app` commands is using the `/build` folder for input. (example: during the deploy process). So copying is the safest option I see. PS: If there are no plans for using the the default `/build` folder for input, I agree with you - just renaming it will do the same trick. – Kaloyan Kosev Jan 06 '17 at 09:56
  • Same for me. I'd like my build folder to go somewhere else so I can create a Docker image without all the other cruft in the root folder to have to be excluded from the docker context. – Ray Booysen Jul 04 '19 at 03:45
  • 1
    The problem with convention over configuration is if you have multiple colliding conventions. IE we use react inside an angular application. So only the final build should go into build and react creates just intermediate files. – paul23 Sep 21 '20 at 12:01
  • 2
    This has landed with version `v4.0.2`. Working example: https://stackoverflow.com/a/66036117/6774916 – t_dom93 Feb 22 '21 at 16:02
  • 2
    "Convention over Configuration" doesn't mean inflexible. – Reactgular Jun 26 '21 at 10:30
  • They figured out "Convention over Configuration" wasn't good enough and implemented it on `v4.0.2` – Coal Jul 21 '21 at 17:17
  • The accepted answer should be @t_dom93 now – Arnaud P Jan 07 '22 at 16:17
44

Support for BUILD_PATH just landed into v4.0.2.

Add BUILD_PATH variable to .env file and run build script command:

// .env file
BUILD_PATH=foo 

That should place all build files into foo folder.

t_dom93
  • 10,226
  • 1
  • 52
  • 38
22

Félix's answer is correct and upvoted, backed-up by Dan Abramov himself.

But for those who would like to change the structure of the output itself (within the build folder), one can run post-build commands with the help of postbuild, which automatically runs after the build script defined in the package.json file.

The example below changes it from static/ to user/static/, moving files and updating file references on relevant files (full gist here):

package.json

{
  "name": "your-project",
  "version": "0.0.1",
  [...]
  "scripts": {
    "build": "react-scripts build",
    "postbuild": "./postbuild.sh",
    [...]
  },
}

postbuild.sh

#!/bin/bash

# The purpose of this script is to do things with files generated by
# 'create-react-app' after 'build' is run.
# 1. Move files to a new directory called 'user'
#    The resulting structure is 'build/user/static/<etc>'
# 2. Update reference on generated files from
#    static/<etc>
#     to
#    user/static/<etc>
#
# More details on: https://github.com/facebook/create-react-app/issues/3824

# Browse into './build/' directory
cd build
# Create './user/' directory
echo '1/4 Create "user" directory'
mkdir user
# Find all files, excluding (through 'grep'):
# - '.',
# - the newly created directory './user/'
# - all content for the directory'./static/'
# Move all matches to the directory './user/'
echo '2/4 Move relevant files'
find . | grep -Ev '^.$|^.\/user$|^.\/static\/.+' | xargs -I{} mv -v {} user
# Browse into './user/' directory
cd user
# Find all files within the folder (not subfolders)
# Replace string 'static/' with 'user/static/' on all files that match the 'find'
# ('sed' requires one to create backup files on OSX, so we do that)
echo '3/4 Replace file references'
find . -type f -maxdepth 1 | LC_ALL=C xargs -I{} sed -i.backup -e 's,static/,user/static/,g' {}
# Delete '*.backup' files created in the last process
echo '4/4 Clean up'
find . -name '*.backup' -type f -delete
# Done
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Wallace Sidhrée
  • 11,221
  • 6
  • 47
  • 58
10

I had the scenario like want to rename the folder and change the build output location, and used below code in the package.json with the latest version

"build": "react-scripts build && mv build ../my_bundles"
Jaison James
  • 4,414
  • 4
  • 42
  • 54
  • this is very useful when copying the contents of a `build` within a `docs` dir for hosting with github pages, ended up going with, `"build-docs": "react-scripts build && cp -r build/** docs"` – ipatch May 16 '20 at 23:16
9

Here is my solution: create .env in root, then add this line to it.

BUILD_PATH=$npm_package_name-$npm_package_version

the build path will be "name_of_app"-"version"

these values could be set in package.json

{
  "name": "my-app",
  "version": "0.1.2",
...
}
5

Based on the answers by Ben Carp and Wallace Sidhrée:

This is what I use to copy my entire build folder to my wamp public folder.

package.json

{
  "name": "[your project name]",
  "homepage": "http://localhost/[your project name]/",
  "version": "0.0.1",
  [...]
  "scripts": {
    "build": "react-scripts build",
    "postbuild": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./post_build.ps1",
    [...]
  },
}

post_build.ps1

Copy-Item "./build/*" -Destination "C:/wamp64/www/[your project name]" -Recurse -force

The homepage line is only needed if you are deploying to a subfolder on your server (See This answer from another question).

gcoulby
  • 534
  • 2
  • 10
  • 20
5

Move command for windows did not work for me. Because it does not copy the "static" folder and subfolders. So I was able to solve this problem using 'ROBOCOPY'.

"build": "react-scripts build && ROBOCOPY build ../my-relative-path/react-app /E",
Muhammet Can TONBUL
  • 3,217
  • 3
  • 25
  • 37
5

Using cross-env is the solution.

Install cross-env:

npm install cross-env

You should update to:

"scripts": {
  "build": "cross-env BUILD_PATH='../yourCustomBuildFolder' react-scripts build",
}
Ilias
  • 71
  • 3
  • 7
4

Quick compatibility build script (also works on Windows):

"build": "react-scripts build && rm -rf docs && mv build docs"
Sagar Ghimire
  • 231
  • 4
  • 10
4

For anyone still looking for an answer that works on both Linux and Windows:

Add this to the scripts section in package.json

"build": "react-scripts build && mv build ../docs || move build ../docs",

with ../docs is the relative folder you want to move the build folder to

Hunter Tran
  • 13,257
  • 2
  • 14
  • 23
2

Windows Powershell Script

//package.json
"scripts": {
    "postbuildNamingScript": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./powerShellPostBuildScript.ps1",


// powerShellPostBuildScript.ps1
move build/static/js build/new-folder-name 
(Get-Content build/index.html).replace('static/js', 'new-folder-name') | Set-Content 
build/index.html
"Finished Running BuildScript"

Running npm run postbuildNamingScript in powershell will move the JS files to build/new-folder-name and point to the new location from index.html.

Ben Carp
  • 24,214
  • 9
  • 60
  • 72
1

Open Command Prompt inside your Application's source. Run the Command

npm run eject

Open your scripts/build.js file and add this at the beginning of the file after 'use strict' line

'use strict';
....
process.env.PUBLIC_URL = './' 
// Provide the current path
.....

enter image description here

Open your config/paths.js and modify the buildApp property in the exports object to your destination folder. (Here, I provide 'react-app-scss' as the destination folder)

module.exports = {
.....
appBuild: resolveApp('build/react-app-scss'),
.....
}

enter image description here

Run

npm run build

Note: Running Platform dependent scripts are not advisable

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Vignesh M
  • 85
  • 1
  • 9
0

You have two possibilities:

  1. Change in your package.json the script item into "build": "react-scripts build && mv build webapp" where webapp is your destination folder;
  2. Create .env file in your root directory and insert in it the new definition of build destination folder (ex. BUILD_PATH='./data')
-1

You can update the configuration with a little hack, under your root directory:

  1. npm run eject
  2. config/webpack.config.prod.js - line 61 - change path to: __dirname + './../--your directory of choice--'
  3. config/paths.js - line 68 - update to resolveApp('./--your directory of choice--')

replace --your directory of choice-- with the folder directory you want it to build on

note the path I provided can be a bit dirty, but this is all you need to do to modify the configuration.

Jerry Lin
  • 57
  • 1
-2

webpack =>

renamed as build to dist

output: {
      filename: '[name].bundle.js',
      path: path.resolve(__dirname, 'dist'),
    },
Anupam Maurya
  • 1,927
  • 22
  • 26