Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rimraf in package.json error #102

Closed
AaronNGray opened this issue Feb 29, 2016 · 16 comments
Closed

rimraf in package.json error #102

AaronNGray opened this issue Feb 29, 2016 · 16 comments

Comments

@AaronNGray
Copy link

Hi, I am using rimraf as a 'npm run clean' command in package.json on Windows 10 it does delete the node_modules directory but I am getting the following error :-

C:\Users\Aaron\Examples\HapiJS\simple>npm run clean

> simple-vhost-example@1.0.0 clean C:\Users\Aaron\Examples\HapiJS\simple
> rimraf node_modules

The system cannot find the path specified.

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "clean"
npm ERR! node v5.7.0
npm ERR! npm  v3.6.0
npm ERR! code ELIFECYCLE
npm ERR! simple-vhost-example@1.0.0 clean: `rimraf node_modules`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the simple-vhost-example@1.0.0 clean script 'rimraf node_modules'.
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 simple-vhost-example package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     rimraf node_modules
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs simple-vhost-example
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls simple-vhost-example
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\Aaron\Examples\HapiJS\simple\npm-debug.log

with the package.json file :-

{
  "name": "simple-vhost-example",
  "version": "1.0.0",
  "description": "Simple Hapi.js vhost example",
  "main": "index.js",
  "dependencies": {
    "glue": "^3.2.0",
    "hapi": "^13.0.0",
    "hoek": "^3.0.4"
  },
  "devDependencies": {
    "rimraf": "^2.5.2"
  },
  "scripts": {
    "start": "node index.js",
    "clean": "rimraf node_modules",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "AaronNGray",
  "license": "MIT"
}
C:\Users\Aaron\Examples\HapiJS\simple>type npm-debug.log
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'clean' ]
2 info using npm@3.6.0
3 info using node@v5.7.0
4 verbose run-script [ 'preclean', 'clean', 'postclean' ]
5 info lifecycle simple-vhost-example@1.0.0~preclean: simple-vhost-example@1.0.0
6 silly lifecycle simple-vhost-example@1.0.0~preclean: no script for preclean, continuing
7 info lifecycle simple-vhost-example@1.0.0~clean: simple-vhost-example@1.0.0
8 verbose lifecycle simple-vhost-example@1.0.0~clean: unsafe-perm in lifecycle true
9 verbose lifecycle simple-vhost-example@1.0.0~clean: PATH: C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin;C:\Users\Aaron\Examples\HapiJS\simple\node_modules\.bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\GNU\GnuPG\pub;C:\Program Files (x86)\Skype\Phone\;C:\GnuWin32\bin;C:\Program Files\nodejs\;C:\Users\Aaron\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\Git\cmd;C:\Utils;C:\Ruby21-x64\bin;C:\Program Files (x86)\Nmap;C:\Users\Aaron\AppData\Roaming\npm;C:\Program Files (x86)\LLVM\bin
10 verbose lifecycle simple-vhost-example@1.0.0~clean: CWD: C:\Users\Aaron\Examples\HapiJS\simple
11 silly lifecycle simple-vhost-example@1.0.0~clean: Args: [ '/d /s /c', 'rimraf node_modules' ]
12 silly lifecycle simple-vhost-example@1.0.0~clean: Returned: code: 1  signal: null
13 info lifecycle simple-vhost-example@1.0.0~clean: Failed to exec clean script
14 verbose stack Error: simple-vhost-example@1.0.0 clean: `rimraf node_modules`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:232:16)
14 verbose stack     at emitTwo (events.js:100:13)
14 verbose stack     at EventEmitter.emit (events.js:185:7)
14 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:24:14)
14 verbose stack     at emitTwo (events.js:100:13)
14 verbose stack     at ChildProcess.emit (events.js:185:7)
14 verbose stack     at maybeClose (internal/child_process.js:827:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
15 verbose pkgid simple-vhost-example@1.0.0
16 verbose cwd C:\Users\Aaron\Examples\HapiJS\simple
17 error Windows_NT 10.0.10586
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "clean"
19 error node v5.7.0
20 error npm  v3.6.0
21 error code ELIFECYCLE
22 error simple-vhost-example@1.0.0 clean: `rimraf node_modules`
22 error Exit status 1
23 error Failed at the simple-vhost-example@1.0.0 clean script 'rimraf node_modules'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the simple-vhost-example package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     rimraf node_modules
23 error You can get information on how to open an issue for this project with:
23 error     npm bugs simple-vhost-example
23 error Or if that isn't available, you can get their info via:
23 error     npm owner ls simple-vhost-example
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]
@AaronNGray AaronNGray changed the title rimraf in package.json rimraf in package.json error Feb 29, 2016
@AaronNGray
Copy link
Author

It only does it on Windows not on Debian Linux

@dsebastien
Copy link

I'm also noticing this issue. What can I do to help troubleshoot this?

@isaacs
Copy link
Owner

isaacs commented Apr 9, 2016

@AaronNGray So, let me see if I understand this correctly:

  1. You have rimraf installed in node_modules
  2. An npm package.json script is calling rimraf node_modules

What may be occurring here is that Windows is getting upset with you trying to unlink a file that is currently in use, and thus locked. (Unix filesystems do not have this limitation.)

@Restuta
Copy link

Restuta commented Jun 9, 2016

@isaacs yes this is exactly the case here. Idk how to deal with it though.

@isaacs
Copy link
Owner

isaacs commented Jul 5, 2016

@Restuta @AaronNGray Can you change the script to do rimraf "node_modules/!(rimraf|.bin)" so that it doesn't try to delete the thing that's currently in use?

@etiennejcharles
Copy link

etiennejcharles commented Aug 10, 2016

Temporary Workaround: One of the solutions would also be able to install rimraf as a global package, after running npm install by including the following line in your package.json.

"postinstall":"npm install rimraf -g",

choonchernlim pushed a commit to choonchernlim/front-end-stack that referenced this issue Sep 28, 2016
… `.bin` within `node_modules` to prevent Windows from throwing an error. See isaacs/rimraf#102
@hegdeashwin
Copy link

I am working on Windows 10, the temporary solution "postinstall" provided by @etiennejcharles is not working in my case.

@binki
Copy link

binki commented Apr 20, 2017

@hegdeashwin I think that if your project has a(n) (in)direct dependency on rimraf, npm run will prefer the version installed in node_modules/.bin over the one installed globally. Then you’ll end up with this same issue of rimraf trying to delete locked files.

Trying to run a script that deletes itself on Windows just isn’t a clean thing to do (unless you use whatever tricks Cygwin’s rm does). And if rimraf or any of its dependencies ever uses a lazy require() you’d end up with problems from partially deleted node_modules on any platform. You should use some tool that is not installed in your local project’s node_modules to remove your local project’s node_modules directory. This technique should be reliable and work on all platforms. I personally avoid writing such a script as an npm run script in my project because I know this technique is not portable.

If you choose to use rimraf, then to use a globally installed version (e.g. installed by npm install -g rimraf), you must invoke it from outside of npm run. This is because npm run adds node_modules/.bin to PATH prior to running scripts. E.g., at the terminal you would type rimraf node_modules instead of npm run clean. If your clean step is more complicated, you could make it its own module and binary and install it globally or in another folder so that you would type my-clean-script instead of npm run clean.

@kylecordes
Copy link

@hegdeashwin @binki The notion of being able to delete node modules itself, especially on windows, has come up many times in conversation (and I think more than a few times in this issue tracker). I believe the conclusion is been that the ability to do so is outside the scope of rimraf, unless you want to require that it be installed globally and banish it from being installed locally (because of the preference problem).

To workaround all of this, I created a separate standalone single file bundle of rimraf:

https://www.npmjs.com/package/rimraf-standalone

wget https://unpkg.com/rimraf-standalone@2.6.1/rimraf-standalone.js
  "scripts": {
    "clean": "node rimraf-standalone.js node_modules"
  },

Follow the link above for all the details.

@IAMtheIAM
Copy link

IAMtheIAM commented Oct 4, 2017

@kylecordes Thanks for that link, its exactly what I was looking for. Standalone rimraf that works to remove node_modules

Update: For some reason the script fails with The system cannot find the specified path if trying to remove node_modules and it contains a package called npm-check-updates.

I have about 100 packages and it works fine to remove them all, as long as npm-check-updates isnt installed. But if I install that package then the script fails. That's weird, how is that possible? I'm running node rimraf-standalone.js node_modules

For a solution I just opted to install that package globally and it works fine now.

@Mottie
Copy link

Mottie commented Aug 12, 2018

I've encountered this issue as well. When I use the aforementioned script (#102 (comment)) in the package.json file:

"scripts": {
  "clean": "rimraf 'node_modules/!(rimraf|.bin)'"
}

I'm seeing this error when the script is executed (Windows 10):

> rimraf node_modules/!(rimraf|.bin)

'.bin)'' is not recognized as an internal or external command,
operable program or batch file.

When I create a temp.js file, and execute it, it performs as expected:

#!/usr/bin/env node
'use strict';

const fs = require('fs');
const rimraf = require('rimraf');

rimraf('node_modules/!(rimraf|.bin)', fs, () => {});

I believe that the vertical bar in the glob is being treated as a pipe. Anyone know of a way around this problem?

@nadavsinai
Copy link

nadavsinai commented Oct 7, 2018

@Mottie
escape and use double quotes.

"clean": "rimraf \"node_modules/!(rimraf|.bin)\""

@IAMtheIAM
Copy link

IAMtheIAM commented Nov 16, 2018

If you are unable or unwilling to install rimraf globally, the best solution is to use the new npx command so it installs in a temporarily path and runs it, after which it gets deleted. Because it is in its own temp folder, it doesn't try to delete itself.
npx rimraf node_modules

@DerekNonGeneric
Copy link

@IAMtheIAM's suggestion to use npx is a good one, the only problem is that npx attempts to use the locally installed dependencies if they exist, which causes this problem to persist. The command that actually needs to be run is npx --ignore-existing --quiet rimraf package-lock.json node_modules. Hope that helps someone out!

@isaacs
Copy link
Owner

isaacs commented Jan 13, 2023

Glob support dropped in v4. Just give it the arguments for the thing you want deleted. Note that shell expansion is VERY different on windows from how it is on the posix shell.

@isaacs isaacs closed this as completed Jan 13, 2023
@brusayannick
Copy link

I had this issue and running sudo before the command fixed it for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.