score:58
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:
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.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!
score:-2
webpack =>
renamed as build to dist
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
score:-1
You can update the configuration with a little hack, under your root directory:
- npm run eject
- config/webpack.config.prod.js - line 61 - change path to: __dirname + './../--your directory of choice--'
- 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.
score:0
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
.....
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'),
.....
}
Run
npm run build
Note: Running Platform dependent scripts are not advisable
score: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
.
score:2
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",
}
score:3
Quick compatibility build script (also works on Windows):
"build": "react-scripts build && rm -rf docs && mv build docs"
score:3
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
score: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).
score: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",
score:6
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",
...
}
score: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"
score:17
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
score:35
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.
score:70
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/...
.
score:111
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.
score:133
Edit your package.json:
"build": "react-scripts build && mv build webapp"
Source: stackoverflow.com
Related Query
- Use custom build output folder when using create-react-app
- When to use a react framework such as Next or Gatsby vs Create React App
- How to use absolute path to import custom scss, when using react + webpack?
- Custom Proxy in Create React App using Typescript
- The app build fails when using React Native CLI
- Is it possible to use experimental version of React while using Create React App
- When I try to use Parcel to build a production version of a React app that works fine in development mode, I get "Cannot find module 'sass'"
- Receiving error when creating new app using create react app
- I create a build of my react app and connected with electron.js. now how to connect my backend from server folder with electron
- Issue with .env being undefinde when using create react app --template typescript?
- express * all routes never runs with create react app when using app.use(express.static)
- Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component when using Create React App and Formik
- Not able to create an optimized build for my React app in the build folder
- How to create a React app directly in the current folder
- How to create multiple page app using react
- You should not use Route or withRouter() outside a Router when using react-router 4 and styled-component in react
- Cannot find module 'caniuse-lite/dist/unpacker/agents' when running create react app
- Missing Script when I run npm start to create React app
- How to show build datetime on my react web app using create-react-app?
- Create React App 4.0 cannot resolve image path in public folder
- Swiper React | How to create custom navigation/pagination components using React refs?
- How to include custom JS files in to React create app
- Share codebase using common Sdk module in create react app Reactjs application
- Why does production build of React app (with Webpack and Babel) use wrong development env with HMR, which causes errors?
- How to inject port and host using ENV variable in Create React App Proxy settings?
- create react app not loading css background images in dev or build
- What's the easiest way to use Create React App with Relay?
- How to use yarn to create a React app project?
- create react app Configuration file after build app
- How to create a preview (Image and description) to the Url when shared link on Social media,Gmail and Skype using React JS?
More Query from same tag
- Abstract generic react class missing prop
- calling useState setter function after async API call
- React component displaying dynamic data based on URL
- How to push to state in React Hooks?
- Semantic-UI-react fixed sidebar
- ref.current is null in react
- AJAX finish after my render() component - ReactJS - React-Router - JQuery -
- How to insert SVG file using React , having it's path/url?
- react leaflet, pass marker's e.latlng property to formik
- call child function from parent in reactjs
- HTTP ERROR 405 when deploying React frontend with Spring boot REST API backend
- Error in setting up ReactJs
- Unable to setState on props in react functional component
- Call custom React async hook within a component and se it's state again
- Test exception - unstable_flushDiscreteUpdates
- Mongoose .save() not saving to database?
- How to organize multiple checkboxes?
- Race condition in React setState and Promise
- How to validate one input field + search-button with Formik in React?
- Route to different page[react-router v4]
- Prevent loading options in SELECT drop down until minimum characters are entered
- React Hook Form with file just submitting fakepath
- Conditional input placeholder with special characters (HEX value)
- How to Lazy load the background image inside the inline style property (React)?
- Antd Form with React hook form
- cann't decrease a value by one in the UI with node.js mongodb, react
- webpack configuration for react CSS
- Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. Even with arrow functions used
- Why does Gatsby no longer recognize components in site-directory?
- ASP.NET Core API and React JS