TetraDev TetraDev - 5 months ago 107x
Bash Question

NPM rimraf - continue executing when folders are missing (stop exit code 1)

I have an npm script set up to a few synchronous commands. The starting command is

npm run clean:install"

Here is the sequence:

"install:all": "npm install && bower install",
"clean": "npm run rimraf -- node_modules doc typings coverage wwwroot bower_components"
"preclean:install": "npm run clean",
"clean:install": "npm set progress=false && npm run install:all"

Works fine if all directories exist. The problem is if any of the directories are already deleted, the script exists with code 1 and prevents all additional syncronous scripts from running.

So that means it failes to run the original command of clean:install which is "
"clean:install": "npm set progress=false && npm run install:all

Error from npm:

npm ERR! angular2-webpack-starter@5.0.4 clean: `npm cache clean && npm run
rimraf -- node_modules doc typings coverage wwwroot bower_components`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the angular2-webpack-starter@5.0.4 clean script 'npm cache clean && npm run rimraf -- node_modules doc typings coverage wwwroot bower_components'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the angular2-webpack-starter package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm cache clean && npm run rimraf -- node_modules doc typings coverage wwwroot bower_components
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs angular2-webpack-starter
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls angular2-webpack-starter
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

The last line gives it away. It wants
but can't find it so it fails.

How can I make my script continue executing to ignore these failures (missing directories)?

EDIT: The error occurs because rimraf is removing
and wiping itself out. Once the folder it cleared, rimraf is gone and exit code 1. Can I exclude the rimraf folder within node_modules so it exists correctly?


I am (probably) looking at this boilerplate project you're using. Did you read the instructions? They are asking you to run npm install typings webpack-dev-server rimraf webpack -g, which includes installing rimraf globally, before even using the project. If you've failed to do this, using the npm run clean:install, or anything which calls this script, will fail because the rimraf module can't be found in your local node_modules (if already removed) or it will be deleting itself.