NPM install doesn't delete /tmp on successful exit when installing packages that have shrinkwrap

4k views Asked by At

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

There are 2 answers

5
IBam On BEST ANSWER

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.

3
nemisj On

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