简体   繁体   中英

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 node_modules 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 node_modules 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.

The solution was a bit tricky. The problem was because rimraf was removing itself since it was only installed locally in node_modules. So as soon as it removed the node_modules dir, the program died.

I realized it was necessary to add a task to have it install rimraf globally. The first task that executes is install:all . Then rimraf is in the global PATH. However, rimraf was a dependency for many other node_modules, so it kept getting installed locally and therefore ignored the global PATH. It kept dying because it was still referencing the local node_modules folder.

The trick was to uninstall rimraf locally right before executing any rimraf command. Now it's forced to use the global PATH because it doesn't exist locally.

"preinstall:all": "npm install typings webpack-dev-server rimraf webpack -g",
"install:all": "npm install && bower install",

"prerimraf": "npm uninstall rimraf",
"rimraf": "rimraf",

"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"

After install:all has been executed on project load (in Visual Studio 2015, Task Runner Explorer has a binding called Project Open ), there are times I want to clean everything and start fresh. Now I can simply execute clean:install and uninstall node_modules and reinstall immediately with no problem! Exit code 0. Sweet!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM