If I run npm build the build files are getting generated in the home directory of the project. Is there any way I can specify custom directory to generate build outside the home directory of the project?
Use case : I want to maintain build files and the development code in different repositories so that it will be easy for me to push to my CDN
There is no such option but you can work around it. Change build in your package.json scripts to perform an additional step, like react-scripts build && cp -r build ../your-folder. If you're on Windows the command would look differently but the idea is the same. There are also ways to write it in a cross platform way if you remove spaces before and after ampersands and launch a Node script that copies files instead of a UNIX command. I hope this helps!
I guess this helps thanks :) but it would be great if we can pass configs to the script. Can we have it as feature request?
We generally don鈥檛 offer configuration options. Please see this document for more information.
This is intentional because it lets other tools integrate with Create React App more tightly because they can make assumptions about where sources and build output are. Since it is _always_ possible to add this on top as an additional script, we don鈥檛 see the value in this being configurable, and it鈥檚 not worth the downsides in our opinion.
(Oops, didn鈥檛 mean to reopen. Fat finger.)
@gaearon The biggest value for this type of configuration option is convenience - it's much easier to set a flag than it is to copy the files and delete the build directory, especially when that script is different between Mac and Windows.
You could always go the configuration file route similar to .angular-cli.json which would still enable other scripts to always know the build directory by reading that file.
For cross-platform dev:
"scripts": {
"windows-build": "react-scripts build && xcopy /f /y build wwwroot",
"mac-build": "react-scripts build && cp -r build ../wwwroot"
}
Edit:
If you switch to use either Git Bash or Ubuntu Bash on Windows, the Unix commands work just fine:
Example which builds the react app, copies the contents from the /build folder to the /wwwroot directory and removes the build directory:
"scripts": {
"build": "react-scripts build && cp -r build/. wwwroot && rm -R build",
}
how would you write
"scripts": {
"build": "react-scripts build && cp -r build/. wwwroot && rm -R build",
}
for Windows.
I tried with xcopy, but when I npm run build
where
"scripts": {
"build": " "react-scripts build && xcopy C:\FolderOne C:\FolderTwo",
}
I get this error in the command line:
npm ERR! Failed to parse json
npm ERR! Unexpected token ' in JSON at position 212 while parsing near '...-env.dev",
npm ERR! "build": '"xcopy C:\FolderOne...'
Most helpful comment
There is no such option but you can work around it. Change
buildin yourpackage.jsonscriptsto perform an additional step, likereact-scripts build && cp -r build ../your-folder. If you're on Windows the command would look differently but the idea is the same. There are also ways to write it in a cross platform way if you remove spaces before and after ampersands and launch a Node script that copies files instead of a UNIX command. I hope this helps!