Repository navigation
fails to invoke npm when used in run-scripts #1734
Copy link
Copy link
Closed
Description
Environment
- Platform: MacOS
- Docker Version: 20.10.16
- Node.js Version: 16.15.1
- Image Tag: node:16.15.1-alpine
Expected Behavior
Given the following package.json
{
"name": "test",
"scripts": {
"ver": "npm --version"
},
"devDependencies": {
"cowsay": "^1.5.0"
}
}$ npm run ver
> ver
> npm --version
8.12.1The same behavior should be replicated within a container runtime
Current Behavior
When running within a container runtime, npm fails and returns 243 exit code
$ docker run --rm -v $PWD:/app --workdir /app --entrypoint npm node:16.15.1-alpine run ver
> ver
> npm --version
npm notice
npm notice New minor version of npm available! 8.11.0 -> 8.12.1
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.12.1>
npm notice Run `npm install -g npm@8.12.1` to update!
npm notice
$ echo $?
243When using a different image, it works as expected
$ docker run --rm -v $PWD:/app --workdir /app --entrypoint npm node:14-alpine run ver
> test@ ver /app
> npm --version
6.14.17It works on on some images (note the --user node:node option)
$ docker run --rm -v $PWD:/app --workdir /app --entrypoint npm node:16.15.0-alpine3.15@sha256:bb776153f81d6e931211e3cadd7eef92c811e7086993b685d1f40242d486b9bb run ver
> ver
> npm --version
8.5.5
npm notice
npm notice New minor version of npm available! 8.5.5 -> 8.12.1
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.12.1>
npm notice Run `npm install -g npm@8.12.1` to update!
npm notice
$ docker run --rm -v $PWD:/app --workdir /app --entrypoint npm --user node:node node:18-alpine run ver
> ver
> npm --version
8.11.0when the node user is used, then there a permission issue when mount volumes are used.
$ docker run --rm -it -v $PWD:/src -v node_modules:/src/node_modules --workdir /src --user node:node --entrypoint /bin/sh node:lts-alpine -c 'npm install && npm run ver'
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /src/node_modules/ansi-regex
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/src/node_modules/ansi-regex'
npm ERR! [Error: EACCES: permission denied, mkdir '/src/node_modules/ansi-regex'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/src/node_modules/ansi-regex'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/node/.npm/_logs/2022-06-09T07_10_27_665Z-debug-0.log
$ docker run --rm -it -v $PWD:/src -v node_modules:/src/node_modules --workdir /src --user root:root --entrypoint /bin/sh node:lts-alpine -c 'npm install && npm run ver'
added 41 packages, and audited 42 packages in 2s
3 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.11.0 -> 8.12.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.12.1
npm notice Run npm install -g npm@8.12.1 to update!
npm notice
> ver
> npm --version
Steps to Reproduce
Use the package.json configuration above and repeat the commands in this post.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
alaney commentedon Jun 7, 2022
Seeing the same issue in my Jenkins pipeline with
node:16.15-alpine3.15. I cannot reproduce the issue locally though.parkwart commentedon Jun 8, 2022
we are facing the same issue.
on k8s we're setting the pod security context to run with an arbitrary user id, after deleting this in the config the pod starts again.
we are setting the tag / sha of images to node:16.14 for now, to keep our pod settings.
also see npm/cli#4996
reidja commentedon Jun 8, 2022
Also seeing this issue, hard coding to
node:16.15.0-alpinein the meantime.gcstarr commentedon Jun 10, 2022
This is our circumstance as well. We use an arbitrary non-privileged user ID which worked fine until v16.15.1.
electriquo commentedon Jun 14, 2022
Maybe @nschonni knows what's up
neonidian commentedon Jun 15, 2022
Facing the same issue.
Was using
node:16-slimwhich brought in version 16.15.1-slim. So reverted back to previous minor version as a temporary fix by specifyingnode:16.14-slimcascornelissen commentedon Jun 22, 2022
For us this was specifically with the
16.15.1patch release,16.15.0works in our case.scooper91 commentedon Jun 27, 2022
I'm having a similar issue. We're seeing it when we try to do an
npm installfrom the Docker command, when we set the user for the container. If you set the node cache to a non-existent path, it seems to work.Can replicate it using a simple
package.jsonfile with a dependency. This example assumes thepackage.jsonis owned by user 1001.When setting
--cache=nope, the install works:EDIT:
It appears (at least in part) to do with the home directory for the user it's running as. Running as user 1001 (which doesn't exist in the container) fails to run
npmat all:If you add the user with that ID, it creates the home directory and works:
If you add the user with that ID, but without the home directory, you get the same error:
Hardcode node version to 16.15.0 in tests
yuki-js commentedon Jun 28, 2022
It seems that
npm execand other command-invoking command such asnpxexecutes commands as the owner of the current working directory, regardless of the user that these commands executed.Edit: here overrides uid if root
https://github.com/npm/cli/blob/9f94049f058687b916da726ea625b5fa68d0829d/node_modules/%40npmcli/promise-spawn/lib/index.js#L13
SmallhillCZ commentedon Jun 29, 2022
Probably the same problem with docker image of 18.2-alpine, npm version 8.9.0.
{ "name": "test-app", "version": "0.0.0", "scripts": { "test": "npm --version" }, ... }The issue here seems npm doesnt want to run as
root. Example when container is run asrootand when switched to usernodeusingsu:Running the container as
nodefixes the issue.electriquo commentedon Jun 29, 2022
@SmallhillCZ
Not fully correct. It depends whether you are using the container in conjunction with mount/data volumes as
node_modulesdirectory. When it is used, the user must berootin most cases (depends on the inner implementation of how mount volumes work across different operating systems)electriquo commentedon Jul 6, 2022
The issue exists in other images too and seems like the maintainers/contributors are not a part of the thread discussion.
Do we know what is the cause for the issue?
Is anyone working on a fix?
yuki-js commentedon Jul 10, 2022
@foolioo
NPM overrides uid if current user is root in the following line.
https://github.com/npm/cli/blob/9f94049f058687b916da726ea625b5fa68d0829d/node_modules/%40npmcli/promise-spawn/lib/index.js#L13
This was removed before, but reintroduced now.
npm/promise-spawn#6
electriquo commentedon Jul 10, 2022
@yuki-js Thanks. Do you have any suggestion how to resolve the issue?
9 remaining items
Temporary fix for nodejs/docker-node#1734
neon010 commentedon Sep 14, 2022
The simple solution is to change the nodejs version. I was facing the same issue, i fixed it by changing node version from 16 to 14 and this worked
DaleCam commentedon Sep 16, 2022
Simply adding
user: nodeto my docker-compose.yaml fixed it in my case...ViliusS commentedon Oct 10, 2022
This is still happening on 16.17.1 docker images.
ViliusS commentedon Oct 10, 2022
Actually, this is not only an issue with run-scripts. node:16-alpine docker image is completely broken as a base build image in these cases:
readOnlyRootFilesystem: true.The source of these issues is that
npm_config_prefixis set to/usr/localandnpm_config_userconfigis not set at all, so NPM is trying to use HOME folder which in the docker image is set to/. Again, that folder is not writable by any of the users except root, but remember, NPM ignores root user due to https://github.com/npm/cli/blob/37bf6d31bd2f68e661928744833b57dcabbb0d1a/node_modules/@npmcli/promise-spawn/lib/index.js#L13The only way I found to fix this on Jenkins is set
NPM_CONFIG_CACHEto something like/tmp/jenkins/.npm. GitLab/GitHub runners can be fixed in similar manner.I guess the final fix should be to create .npm cache folder somewhere writable by any user on the docker image, so at least it works by default on CI pipelines. Maybe create some kind of documented folder specified via
npm_config_userconfigso anyone can map it and modify the behaviour of a running user.P.S. Why NPM tries to spawn a process according to permission on current folder in case it is run by the root user is still a mistery to me. It goes against all commons. Programs should not modify running user.
nschonni commentedon Oct 10, 2022
https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#non-root-user
ViliusS commentedon Oct 10, 2022
Yes, I saw that, but this assumes that Docker image can be modified. In a lot of cases, you cannot do that. Free GitLab runners are one such example.
sasskinn12 commentedon Oct 11, 2022
I confirm, the issue is still exist in latest 16.17.1 docker images when we run them on Openshift/Kubernetes.
We need to run latest node 16 version images because of security policy we have!
Node 16.15.0 Docker image worked before for us just fine. Now something is changed/broken on later releases.
Seems issue is so old already on nowbody cares to fix it 👎
hannesvdvreken commentedon Oct 11, 2022
cares to fix it
Fix error 243 in GitHub Actions
stall84 commentedon Feb 6, 2023
So I encountered what I believe is this problem (although not sure of the fix yet). Essentially experiencing the exact same behavior. It's probably the most confounding problem I've ever had to troubleshoot because (and I mean this sincerely) "the compose-up command was working with the image last week, and not today", with literally nothing changed in the dockerfile or compose file .. I started trying to re-build the image as fresh as possible (--no-cache, etc) .. and then just started deleting images off docker desktop as verbose build logging still showed a few layers being "CACHED" but I might misunderstand that as it was referencing the node:18.13-alpine image itself.
Anyway.. I was able to get it working both inside the docker desktop GUI (play button/no args) (working btw for us means that NestJS runs the 'start --watch' command (via 'npm run start:dev') starting a NestJS dev server). And then (again without changing a single thing except the final CMD to be "bin/sh" instead of what it was (working with) last week which was ["npm", "run", "start:dev" ] then running 'npm install' again which updated 0 things (as there is already that command further up in the dockerfile.. but just to be sure ran it again .. then ran 'npm run start:dev' and lo and behold it started the nest dev server like it did last week.. without changing anything other than deleting old images using node:18.13-alpine off my system
To add more context as one person mentioned in their reply: The docker compose that ran (and worked last week but not today) did create a volume to the pwd (the app directory) and so I assume there would be node_modules in that mount as my local machine is the 'root' for this project's initial build and docker development environment creation
So here I am more confused than at the start as to what in the heck is going on here.. and can't wait for one of the other devs to have the same problem so I can say "oh yeah .. just start deleting things and starting them and clicking.. it eventually works again"
carloliwanag commentedon May 2, 2023
I also encountered this issue when building an Angular+nginx image. What worked for me is I included the
package-lock.jsonto the Dockerfile.MikeMcC399 commentedon May 3, 2026
The original issue with Node.js 16.15.1 / npm 8.11.0 is reproducible running:
docker run --rm -v $PWD:/app --workdir /app --entrypoint npm node:16.15.1-alpine run verUpdating to Node.js 16.18.0 with bundled npm 8.19.2 resolves the npm issue:
Node.js 16 reached end-of-life on 2023-09-11.
The lowest currently supported Node.js version is 22.22.2 with bundled npm version 10.9.7. The issue is also no longer reproducible with this version:
docker run --rm -v $PWD:/app --workdir /app --entrypoint npm node:22.22.2-alpine run verClosing, as resolved.
ViliusS commentedon May 10, 2026
Unfortunately, this is only fixed partially. Changes in https://github.com/npm/cli/pull/5757/changes now allows this image to be run as root, but it is still broken the same way when running on rootless Docker, or other unprivileged environments. The real issue is really not the permissions, but the incorrectly configured cache folder settings in the image itself. I have created a generalized ticket about this #2495.