55

I am trying to deploy a web project but before build it, I need to clear/delete the dist folder.

I could add a script that would run rm -rf dist/* but that would not work on Windows system.

Is there some npm package u other solution that allows deleting a folder with a command that works in every OS?

Juanma Menendez
  • 17,253
  • 7
  • 59
  • 56
Pablo
  • 9,424
  • 17
  • 55
  • 78
  • 1
    You can use the built-in fs module in node to do this, but I can't tell from your question if you can run a node app as part of the deployment. If you can, https://nodejs.org/api/fs.html will help you write the code. – Jim B. Nov 18 '16 at 17:37
  • is there to run it as a command line, instead of a JS function? – Pablo Nov 18 '16 at 17:51
  • 1
    Yes, if you run it as a node app like: node removeDistDir.js – Jim B. Nov 18 '16 at 18:12
  • 7
    `node -e "require('fs-extra').emptyDir('server/dist');"` – Robert May 12 '20 at 12:34

4 Answers4

57

You can use rimraf: https://github.com/isaacs/rimraf.

Note that if you are using globs containing the globstar (**), you must double-quote them. Unix systems don't all support the globstar by default, but rimraf will expand them for you. Windows doesn't support single-quotes, so those can't be used. Remember that double-quotes must be escaped in JSON with a \.

RyanZim
  • 6,609
  • 1
  • 27
  • 43
  • 3
    Projects [`trash`](https://github.com/sindresorhus/trash) and `del` also support globs; I'd recommend using `trash` unless you're *really* sure you want to permanently delete. – iono Apr 08 '19 at 12:22
  • 5
    Since npm 5.2.0 we can use `"clean": "npx rimraf dist"` (in "scripts" of package.json) – dhilt Jan 05 '21 at 00:15
  • 6
    @dhilt No need to use `npx` in npm scripts; all installed modules are already available there. (i.e. should be `"clean": "rimraf dist"`) `npx` is only needed for manually running modules from the command line. – RyanZim Jan 05 '21 at 18:07
  • @RyanZim The point is that you may not install "rimraf", with "npx" approach you may not include "rimraf" in the dependency list. – dhilt Jan 06 '21 at 00:23
  • 1
    @dhilt Ah, I forgot about that behavior of `rimraf`; though I wouldn't recommend doing that. You want it as a `devDependency` so you're locked to a compatible version. – RyanZim Jan 07 '21 at 19:42
31

Recently I was faced with the same challenge as you, and just like user Kevin Brown stated in a comment above, I was interested in a solution which wasn't just "use this npm package". So I'm throwing this out here hoping someone will find it useful.

So, what I did was taking some code I found in StackOverflow, putting it on a .js file, and binding it to an npm script in my package.json. In my case, the goal was to delete the "/out" folder, where .ts scripts were compiling to, but the possibilities are endless!

With this solution, you only need to start your app with "npm run cleanstart".

package.json

{
    "name": "my_app",
    "version": "1.0.0",
    "main": "main.js",
    "scripts": {
        "tsc": "tsc",
        "precleanstart": "node clean.js",
        "cleanstart": "npm start",
        "prestart": "npm run tsc",
        "start": "node out/main.js"
    },
    "dependencies": {
        "@babel/runtime": "^7.0.0-beta.54",
        "babel-runtime": "^6.26.0",
        "body-parser": "^1.18.3",
        "express": "^4.16.3",
        "typescript": "^3.0.3"
    }
}

clean.js

var fs = require('fs');

function deleteFolderRecursive(path) {
  if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) {
    fs.readdirSync(path).forEach(function(file, index){
      var curPath = path + "/" + file;
      
      if (fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    
    console.log(`Deleting directory "${path}"...`);
    fs.rmdirSync(path);
  }
}

console.log("Cleaning working tree...");

deleteFolderRecursive("./out");

console.log("Successfully cleaned working tree!");
Aardvark
  • 8,474
  • 7
  • 46
  • 64
Tharkius
  • 2,744
  • 2
  • 20
  • 31
27
npm install --save-dev del-cli 

"scripts": {
    "delete": "del-cli --force ../folder-to-delete"
}

notice "--force" when you want to delete a parent folder

Maayan Hope
  • 1,482
  • 17
  • 32
2

I found this today:
npm i clean-webpack-plugin --save-dev

plugins: [
    ...
    new CleanWebpackPlugin(['dist'])
 ]

FYI: https://www.npmjs.com/package/clean-webpack-plugin

Daniel De León
  • 13,196
  • 5
  • 87
  • 72
  • 1
    no need an array inside the options. it's a json nowadays – Soldeplata Saketos May 27 '20 at 13:12
  • it also seems that the plugin cleans `output.path` from the WebPack config, and i don't see a way for a user to specify otherwise. In my case, output.path is a subdirectory of what I want to clean, so I don't think the plugin is a good fit. I could be misreading the docs! – Joe Germuska Jun 17 '20 at 21:59