3

When I try to clean a folder with npm by running npm run clean (code underneath), I get the following error:

rm: cannot remove 'lib/*': No such file or directory

In my package.json, I'm using the following scripts:

{
    "scripts": {
        "clean": "rm -r lib/*",
        "show": "ls lib/*"
    }
}

I'm absolutely sure the folder exists. I've tried using single quotes around the lib path. I know glob expansion is working: the npm run show works perfectly fine.

I do not want to add any dependencies like rimraf as suggested here. I also do not care about windows support. I know I could use rm -r lib && mkdir lib which does work. I'm mostly interested in why rm -r lib/* gives the error. Could someone help me out?

npm version: 5.5.1

node version: 8.9.3

axm__
  • 2,463
  • 1
  • 18
  • 34
  • Maybe script runs in another directory, then the folder exists? Try to `pwd` to print path from script run – xxxvodnikxxx Apr 16 '18 at 14:15
  • 1
    I've actually thought about that, but then why would the `npm run show` command work? To be sure I just tested it with `pwd` in a npm script and it returns the correct folder. – axm__ Apr 16 '18 at 14:18

2 Answers2

7

Are you sure your lib folder is not already empty?

rm -r somedir/*

Gives this error when ran on an empty dir because "*" doesn't exist because the dir is empty.

If you want to remove the lib folder itself you have to type rm -r lib

user3330623
  • 111
  • 3
  • 1
    There is mentioned he knows about that he can remove whole folder and recreate `I know I could use rm -r lib && mkdir lib which does work` but I guess he wants to know why its failing. Good point. – xxxvodnikxxx Apr 16 '18 at 14:26
  • Oh wow. Was pretty sure I've tried that, but I'm wrong. Thank you! Guess I have to do a check first before I delete the folder. – axm__ Apr 16 '18 at 14:27
  • 1
    @xxxvodnikxxx: somehow I forgot one of the very basic things to check. Thank you both for thinking along :) – axm__ Apr 16 '18 at 14:29
5

Use rimraf: Run the unix command rm -rf in Node.js

npm i rimraf -D

and in your script, write

"scripts": {
    "delete:folder": "rimraf lib/*",
}
Alex Montoya
  • 4,697
  • 1
  • 30
  • 31