[BUG] Failed to install npm package from git in docker since v6.11.0
Problem
What / Why This bug is related to https://github.com/npm/cli/issues/476#issuecomment-553917541 and https://github.com/npm/cli/issues/514 When `npm install` packages from git in docker Where packages from public git repositories How Current Behavior [code block] Steps to Reproduce Create simple package.json with git dependency [code block] run `docker run --network=host -it -v $(pwd):/var/work/test -w /var/work/test node:12.14.0 /bin/bash` `ls -l` shows that files are owned by unknown user and unknown group (this is typical behaviour on build agents like teamcity, teamcity clones git repo and mounts it inside docker container) [code block] run `npm install` [code block] run `npm i -g npm@6.10` (downgrade to npm 6.10.X) run `npm install` [code block] Expected Behavior `npm install` / `npm ci` works well like npm@6.10
Error Output
Error while executing:
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix npm package installation from git in Docker for npm v6.11.0+
The issue arises due to changes in npm v6.11.0 that affect how git dependencies are handled, particularly in environments where the user and group IDs are unknown, such as in Docker containers. This leads to permission issues when trying to install packages from public git repositories.
Awaiting Verification
Be the first to verify this fix
- 1
Create a Dockerfile with specific npm version
Create a Dockerfile that specifies an npm version compatible with your project, such as 6.10.0, to avoid the issues introduced in later versions.
dockerfileFROM node:12.14.0 RUN npm install -g npm@6.10.0 - 2
Build the Docker image
Build the Docker image using the Dockerfile created in the previous step to ensure that the correct npm version is installed.
bashdocker build -t my-node-app . - 3
Run the Docker container
Run the Docker container with the built image, ensuring that the current directory is mounted correctly to maintain file permissions.
bashdocker run --network=host -it -v $(pwd):/var/work/test -w /var/work/test my-node-app /bin/bash - 4
Install npm packages
Inside the Docker container, run npm install to install the packages from the package.json, which now should work without errors.
bashnpm install
Validation
Confirm that the npm install command completes successfully without errors. Check that the installed packages are present in the node_modules directory and that there are no permission issues.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep