1

I'm using npm to install some internally developed packages on a Ubuntu 14.04 server. During install, a folder is created under /tmp. According to the docs, the temporary folder should be deleted on successful exit, but it isn't happening. I've checked the exit code, and it's zero as expected, so what else could be causing this? It's clogging up my build server, which currently has quite limited disk space available - I can work round this by using a cronjob to clean up, or by adding more disk space, but I want to know the cause!

Running npm v 1.4.28.

Update: This only happens when the package you are installing has been shrinkwrapped using the npm shrinkwrap command. Confirmed as a problem in npm v 1.4.28 and 2.1.10. See the issue on GitHub.

Edit: I've also run the install in verbose mode, it ends with

npm verb exit [ 0, true ]
npm info ok

There's lots of mention of /tmp in the output in terms of writing to it, and untarring things there, but no obvious attempt to clean it up.

2 Answers 2

10

Apparently this is a bug in npm. I've raised an issue on GitHub. It only happens when you install a shrinkwrapped package.

As a workaround in the meantime, I ended up just running a cronjob that deletes any of these folders that are more than 10 minutes old, by creating a script in /etc/cron.hourly.

#!/bin/bash

# Removes any folder starting with npm- in the /tmp folder.
sudo find /tmp/ -maxdepth 1 -name 'npm-*' -type d -mmin +10 -exec rm -rf {} +

Update

Although this is confirmed as a bug, it doesn't sound like it will be fixed in npm2, so you're left with a choice between using npm3 or a workaround such as the script above. If you're on Windows, there's a Powershell version of the script on the Github page.

5
  • on Mac, you will need find -L for this to work because /tmp is a symlink on Mac.
    – wisbucky
    Commented Jul 12, 2016 at 0:00
  • You can also just rm -rf /tmp/npm-*
    – caarlos0
    Commented Feb 17, 2017 at 12:31
  • @caarlos0 you can't do that, because some of those files may still be in use by running installs.
    – IBam
    Commented Feb 17, 2017 at 14:51
  • @IBam oh yes, but, assuming there is no npm running, thats the easiest way.
    – caarlos0
    Commented Feb 20, 2017 at 20:33
  • The point is to run it in a cronjob, you can't make that assumption! Well, you can but eventually you'll be wrong.
    – IBam
    Commented Feb 21, 2017 at 6:34
1

Add a one-liner to postinstall script inside the package.json, which will run at the end of every npm install:

{ "scripts": { "postinstall": "ppid=$(ps -p ${1:-$$} -o ppid=;); ppid=$(echo ${ppid}|tr -d '[[:space:]]'); if [ -z ${npm_config_tmp} ]; then npm_config_tmp=/tmp; fi; rm -rf \"${npm_config_tmp}\"/npm-${ppid}*" } }

If you don't like one-liners, there is also a separate script for that available.

https://gist.github.com/nemisj/11f6d01ef9638af283d3

3
  • Interesting workaround, but don't you then need to add that to every package.json you are working on?
    – IBam
    Commented Apr 16, 2015 at 12:57
  • Sure, but how much separate packages are you working on? For me it was a better solution, since I don't have to setup stuff on a machine itself and functionality is embedded into the project.
    – nemisj
    Commented Apr 16, 2015 at 14:12
  • I accept that if you're working on very few packages this is fine... as long as you're end-user has bash. May have problems on Windows? I don't know what the result would be then - would the install fail?
    – IBam
    Commented Apr 16, 2015 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.