Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting the contents of folder with rm -r folder_name/* in npm script gives 'no such folder or directory' error

Tags:

shell

node.js

npm

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

like image 893
axm__ Avatar asked Sep 05 '25 16:09

axm__


2 Answers

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

like image 83
user3330623 Avatar answered Sep 08 '25 10:09

user3330623


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/*",
}
like image 26
Alex Montoya Avatar answered Sep 08 '25 10:09

Alex Montoya