You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] npm silently fails when unable to write cache directory #4996
Above is from node:16-alpine-latest in an OpenShift environment (ie. where an arbitrary user id is used).
I traced it down to the following code,
# /usr/local/lib/node_modules/npm/lib/npm.js
243 // mkdir this separately since the logs dir can be set to
244 // a different location. an error here should be surfaced
245 // right away since it will error in cacache later
246 await this.time('npm:load:mkdirpcache', () =>
247 fs.mkdir(this.cache, { recursive: true, owner: 'inherit' }))
Unfortunately my nodejs skills stops here and I'm not really sure what the comment is referring to, nor why we don't see any output from any log.XX code. Adding a catch to above, makes the code work, however I'm not sure if that is the right approach, I'll leave that to someone else.
diff --git a/npm.js b/npm.js
index 2197f11..1b940be 100644
--- a/npm.js
+++ b/npm.js
@@ -244,7 +244,8 @@ class Npm extends EventEmitter {
// a different location. an error here should be surfaced
// right away since it will error in cacache later
await this.time('npm:load:mkdirpcache', () =>
- fs.mkdir(this.cache, { recursive: true, owner: 'inherit' }))
+ fs.mkdir(this.cache, { recursive: true, owner: 'inherit' })
+ .catch((e) => log.warn('cachedir', `could not create cache-dir: ${e}`)))
With the above patch you'll get something like,
$ > npm --version
npm WARN cachedir could not create cache-dir: Error: EACCES: permission denied, mkdir '/.npm'
npm WARN logfile could not create logs-dir: Error: EACCES: permission denied, mkdir '/.npm'
npm WARN logfile could not be created: Error: ENOENT: no such file or directory, open '/.npm/_logs/2022-06-08T14_58_12_496Z-debug-0.log'
8.11.0
At least that would have pointed us in the right direction from the start.
Sorry for the long post, here's a 🥔😄
¯\(ツ)/¯
Reacted by Kenny Lindholm, Andreas, Sanjay M Santhosh and Felix Leupold
Reacted by Kenny Lindholm, FelipeEmerim and Tobi Kremer
We use k8s with readOnlyRootFilesystem: true. We added an emptyDir volume mounted on /tmp and set the npm_config_cache env var to /tmp/npm/. Because the volume is mounted with 777 permissions it solved our problem without downgrading the node image.
Wow! This just broke all of our applications on k8s and it took me half a day to figure out what's going on because there was no output whatsoever (Note to future self: Check npm issues first). Will this be treated as a bug which will be fixed or are we going to have to patch each and every one of our applications? 🙈
Reacted by Nico, Christopher Addison, Yves Galante, Eric Eastwood and James Bromwell
We use k8s with readOnlyRootFilesystem: true. We added an emptyDir volume mounted on /tmp and set the npm_config_cache env var to /tmp/npm/. Because the volume is mounted with 777 permissions it solved our problem without downgrading the node image.
I just realized we had securityContext.runAsUser and securityContext.runAsGroup set to an arbitrary user id in our k8s deployment. After changing this to uid/gid 1000, which is the node user that comes with the node:16-alpine base image, the problem went away.
Activity
[-][BUG] npm silently fails with used nobody[/-][+][BUG] npm silently fails with user nobody[/+]patchon commentedon Jun 8, 2022
The issue seems to be related to the creation of the "cache-directory".
Setting the
npm_config_cacheto a writable path (ie./tmp/) works.Above is from
node:16-alpine-latestin an OpenShift environment (ie. where an arbitrary user id is used).I traced it down to the following code,
Unfortunately my nodejs skills stops here and I'm not really sure what the comment is referring to, nor why we don't see any output from any log.XX code. Adding a catch to above, makes the code work, however I'm not sure if that is the right approach, I'll leave that to someone else.
With the above patch you'll get something like,
At least that would have pointed us in the right direction from the start.
Sorry for the long post, here's a 🥔😄
¯\(ツ)/¯
FelipeEmerim commentedon Jun 8, 2022
We use k8s with
readOnlyRootFilesystem: true. We added an emptyDir volume mounted on /tmp and set thenpm_config_cacheenv var to/tmp/npm/. Because the volume is mounted with 777 permissions it solved our problem without downgrading the node image.soulchild commentedon Jun 8, 2022
Wow! This just broke all of our applications on k8s and it took me half a day to figure out what's going on because there was no output whatsoever (Note to future self: Check npm issues first). Will this be treated as a bug which will be fixed or are we going to have to patch each and every one of our applications? 🙈
set npm_config_cache to allow building with newer npm
chris-addison commentedon Jun 9, 2022
This made us lose two days of dev time. Took over a day to narrow the issue down to npm. Changing the alpine image back to 3.15 fixed it for us.
XhmikosR commentedon Jun 10, 2022
Could this be related/the same as #4838?
soulchild commentedon Jun 13, 2022
I just realized we had
securityContext.runAsUserandsecurityContext.runAsGroupset to an arbitrary user id in our k8s deployment. After changing this to uid/gid1000, which is thenodeuser that comes with thenode:16-alpinebase image, the problem went away.Fix image not running in K8s (ROSA) (#24)
[-][BUG] npm silently fails with user nobody[/-][+][BUG] npm silently fails when unable to write cache directory[/+]fritzy commentedon Jun 30, 2022
It looks like the exit-handler is not properly reporting
ENOENTerrors when lib/npm.js fails to create the cache directory.fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible
fix: don't fail immediately if cache dir is not accessible (#5197)