'How to remove folder with npm before build

I've got a scripts "build": "rimraf dist webpack --progress --config webpack/prod.js", But actually,it removed not dist but all file inside webpack folder. But I need delete only dist

Structure:
 -dist
 -webpack
     -somefiles.js


Solution 1:[1]

Npm scripts are basically the same as running the commands directly in bash. In your case, you are running rimraf dist webpack which means webpack is an argument for rimraf. To separate commands, you can use ; or & if you want to make sure the first command ran successfully before running the second one use && So your script should look like this.

rimraf dist && webpack --progress --config webpack/prod.js

Solution 2:[2]

Quick and easy cross platform solution. No need of any library

In package.json adding a script that employs node.js api to execute via inline execution

add below code in package.json script

{
  ...
  "scripts": {
     "rmdir": "node -e \"var fs = require('fs'); process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true })); process.exit(0);\"",
  }

}

Then execute as below in command window

npm run rmdir -- folder1 folder2

Solution 3:[3]

npm install --save-dev del-cli

"scripts": { "prebuild": "del-cli --force ../folder-to-delete" } notice "--force" when you want to delete a parent folder

Solution 4:[4]

You can clean your output directory before emit by adding one line in your webpack.config.js file

clean: true,

Take a look at webpack output management : https://webpack.js.org/guides/output-management/#cleaning-up-the-dist-folder

i usually use clean: argv.mode === "production"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lukas-reineke
Solution 2 NiRUS
Solution 3 Maayan Hope
Solution 4 saida lachgar