jsonnode.jsbuildnpm

Creating a sub-folder in package.json


In my package.json (from npm), I have the following build command:

"build": "mkdir -p ./build && cp ./src/index.html ./build/ && webpack -p --config webpack.production.config.js"

This command runs just fine and creates a "build" folder with almost everything I need.

However, I also need to make the build command create a sub-directory (in the build folder) called "images" .

I tried a few things, such as the following:

 "build": "mkdir build && mkdir -p ./build/images && cp src/index.html build/ && webpack -p --config webpack.production.config.js"

And it always returns this error

How can I change this build command to create a sub-directory inside the build folder?


Solution

  • This is because of the sub-POSIX-standard Windows mkdir command. Writing portable shell commands this way is difficult and limiting.

    One option would be to install and use the mkdirp module. Then a pure and portable JavaScript mkdirp command will be available to the npm scripts.

    npm install --save-dev mkdirp
    

    Command:

    "build": "mkdirp ./build/images && ..."
    

    In modern node versions, you could even skip the dependency and do something like this:

    "build": "node -e 'fs.mkdirSync(process.argv[1], {recursive: true})' ./build/images && ..."