NPM Installed failed when downloading dependencies from github
I received the following error during npm install:
npm ERR! code 128
npm ERR! Command failed: git clone --mirror -q https://xxx.github.com/xxxx/some-repo/root/.npm/_cacache/tmp/git-clone-4630f3e6/.git
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-4630f3e6/.git'
My package.json has a dependency that is hosted in Github (i.e. git+https://xxx.github.com/xxxx/some-repo#b515a16ccfd0683b5fdc35061d88b103b03f1230)
This is my Dockerfile:
FROM node:10.18.1-alpine as builder
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
# Install required packages for npm install and remove them after use
RUN apk --no-cache --virtual build-dependencies add \
git \
python \
make \
g++ \
&& npm install \
&& apk del build-dependencies
I noticed that if I used node:10.16.3-alpine image, the issue did not occur.
Alpine doesnt have git by default. You need to explicitly install.
Oh, nevermind, I see you did already....
Whats the error you get?
@LaurentGoderre I posted the error message in my original post
Looks like its struggling to create the cache folder structure, but that would be a problem with npm itself. You could try setting:
NPM_CONFIG_PREFIX=/home/.npm
@LaurentGoderre I added ARG NPM_CONFIG_PREFIX=/home/.npm to the Dockerfile but the same error occurred
Is it complaining about the new path?
@LaurentGoderre no..same path /root/.npm I verified that the environment variable is set during the image build
Step 1/42 : FROM node:10.18.1-alpine as builder
---> 547f0f86b4ad
Step 2/42 : ARG NPM_CONFIG_PREFIX=/home/.npm
---> Running in d354cb512bfb
Removing intermediate container d354cb512bfb
---> 6489ddca08bd
Step 3/42 : RUN echo ${NPM_CONFIG_PREFIX}
---> Running in 04feba3354d2
/home/.npm
Removing intermediate container 04feba3354d2
---> 96b8e0cca53b
Step 4/42 : RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
---> Running in 399a992cb910
Tried NPM_CONFIG_PREFIX=/home/.npm npm install as well, same result:
npm ERR! Command failed: git clone --mirror -q https://xxx.github.com/xxxx/some-repo/root/.npm/_cacache/tmp/git-clone-5739e4c8/.git
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-5739e4c8/.git'
npm ERR!
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-01-16T00_56_26_411Z-debug.log
The command '/bin/sh -c apk --no-cache --virtual build-dependencies add git python make g++ && NPM_CONFIG_PREFIX=/home/.npm npm install && apk del build-dependencies' returned a non-zero code: 1
Installing as the root user is always problematic.
We're seeing this same issue (amongst other issues related to commonjs) when running in an Azure Pipelines Docker Task. The issue does not appear with node:10.16, but does with node:10.17, node:10.18, and (obviously) just node:10.
The confusing thing is that this doesn't appear for us when run locally on macOS: all versions of node:10 work without issue.
I did some investigation on this issue to rule out whether this was a bug in Azure Pipelines. Here is what I found.
The issue only occurs when the package-lock.json file is checked in and when one of the dependencies is fetched via git. It seems that starting with node:10.17, there is a permissions issue creating the directory. In order to get this to work, I had to clear out the umask and change the cache directory.
The issue appears to only affect git inside of the node container as I could successfully run 'mkdir -p', but the directory that npm uses as the target directory of the clone is not constant.
As a mitigation, prior to running npm install, I did umask 0 && npm config set cache /tmp/.npm
Both the umask and changing the cache directory were required.
I verified this issue on Docker 19.03.1 on Linux. It works fine on Docker 19.03.5 on OS X
prior to running
npm install, I didumask 0 && npm config set cache /tmp/.npm
I can verify that this works for us. When running a current node:10 (which is node:10.19.0) container on Azure Pipelines in a Linux host, running npm install alone will fail. However, running:
umask 0 && npm config set cache /tmp/.npm && npm install
allows the installation process to proceed without fail.
For the record, we also have a package-lock.json file checked in, as well as a dependency fetched via git.
As mentioned above, this issue only affects us when running on Docker on Linux (everything works fine without modification when run on Docker on macOS or even Node on Linux [no Docker]).
Thanks @jtpetty works for me too