menu

Questions & Answers

ReactJS Change build folder location in Windows

In package.json I am passing the absolute location of the build folder as follows

"script":{
  "build": "set BUILD_PATH=C:/Tomcat/webapps && react-scripts build",
  ...
}

When I run the npm run build command, 2 empty folders are created, and inside that files and folders are getting stored

Example of build folder: C/Tomcat/webapps/../../all_build_files

Answers(1) :

First and foremost it is supposed to be:

"scripts": {
    "build": "[...]",
[...]
} 

eg. scripts instead of script


You could also use build-win instead of just build but I am guessing that it doesn't matter for now.

However, on to your issue, it should work (at least it does for me) when adding this into the .env. Alternatively you can just copy the output of the build folder into some other folder with cp or copy since you are using windows.

// .env file
BUILD_PATH=foo

In the documentation it is also noted that it should be relative to the root of your project and not absolute. Maybe that is where it fails and creates your two weird directories.

Variable Development Production Usage
BUILD_PATH 🚫 Ignored ✅ Used By default, Create React App will output compiled assets to a /build directory adjacent to your /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.

Source: Create React App Documentation