Search code examples
package.jsonxcopy

use xcopy in package.json


I have a react application and a express server. I am serving react app through express server as a static page. I want to copy the build folder from the react application to server each time I build the react project. I am using xcopy, but its not working.

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && xcopy C:\Users\gevi\Scheduler\client\build C:\Users\gevi\Scheduler\Server\build /E/H/C/I",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }, 

Solution

  • Your example is not valid JSON, therefore consider making the following changes:

    • Escape the backslashes, i.e. change all \ to \\
    • Encase the pathnames in JSON escaped double quotes (i.e. \"...\").

    Essentially, redefine your build script as follows:

    "scripts": {
      ...
      "build": "react-scripts build && xcopy \"C:\\Users\\gevi\\Scheduler\\client\\build\" \"C:\\Users\\gevi\\Scheduler\\Server\\build\" /E/H/C/I",
      ...
    }
    

    For clarity purposes the following example uses a circumflex (^) character to indicate each additional change made to the build script (as per above) that you provided in your question:

    "build": "react-scripts build && xcopy \"C:\\Users\\gevi\\Scheduler\\client\\build\" \"C:\\Users\\gevi\\Scheduler\\Server\\build\" /E/H/C/I",
                                           ^^  ^      ^     ^          ^       ^      ^^ ^^  ^      ^     ^          ^       ^      ^^
    

    Please refer to my answer here if you need to change the xcopy options. E.g.

    • You may want to add the /Q option to not display file names in the console while copying.

    • Or, maybe add an escaped backlash at the end of the destination path to ensure that the build folder is created if it doesn't already exist on the server. If that's a requirement then change the destination path in the previous example to:

      \"C:\\Users\\gevi\\Scheduler\\Server\\build\\\"
                                                 ^^