docker-node: Command "npm install -g" causes "ERR! Cannot read property 'path' of null"

attempting to enhance this image by installing Elm globally but encountering issues. It would appear that the installation process for npm is user 500 and owns and restricts access to /usr/local/lib/node_modules by default. Seems like an unnecessary constraint to impose on an already isolated environment.

Investigation revealed:

4337 error Linux 4.4.0-21-generic
4338 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "elm"
4339 error node v7.8.0
4340 error npm  v4.2.0
4341 error path /usr/local/lib/node_modules
4342 error code EACCES
4343 error errno -13
4344 error syscall access
4345 error Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
4345 error  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
4345 error   errno: -13,
4345 error   code: 'EACCES',
4345 error   syscall: 'access',
4345 error   path: '/usr/local/lib/node_modules' }
4346 error Please try running this command again as root/Administrator.
4347 verbose exit [ -13, true ]

and

ls -al /usr/local/lib/node_modules
total 12
drwxrwxr-x  3 500 500 4096 Mar 29 01:27 .
drwxrwxr-x  4 500 500 4096 Mar 29 01:27 ..
drwxrwxr-x 11 500 500 4096 Mar 29 01:27 npm

where

id -nu 500
id: 500: no such user

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 2
  • Comments: 28 (15 by maintainers)

Most upvoted comments

I think this is due to run npm as root. You should run it as the node user

I was facing this issue while building the Angular 6 image with Docker (v18.09.0).

The Proxy variables of node and Docker environment were causing the problem. The solution is to reset httpProxy variable value as told here: Docker doc. Also, set project specific .npmrc like: gist and fix the npm permission issue: gist

Finally, my Dockerfile looks like:

FROM node:10.14.2-alpine
###
#Issue: 'Cannot read property 'startsWith' of null in npm install' error
#Reason: Proxy variables of node and Docker environment were causing the problem.
#Solution: Modify httpProxy variable value as told here: https://docs.docker.com/network/proxy/#configure-the-docker-client
# Also, set project specific .npmrc like : https://gist.github.com/nikhilbchilwant/7243ea4c6f35f28fb44376dac675d285

#RUN apk add tree

WORKDIR /home/node/aas-ui

#Fix for npm permission issue: https://github.com/angular/angular/blob/f8096d499324cf0961f092944bbaedd05364eea1/tools/ngcontainer/Dockerfile#L51
RUN mkdir /home/node/.npm-global \
 && npm config set prefix '/home/node/.npm-global' \
 && echo "export PATH=/home/node/.npm-global/bin:$PATH" >> /home/node/.profile
RUN source /home/node/.profile

RUN chown -R node $(npm config get prefix)
RUN chown -R node /home/node/aas-ui
USER node

COPY src ./src/
COPY e2e ./e2e/
COPY .angular-cli.json ./angular-cli.json
COPY protractor.conf.js ./protractor.conf.js
COPY karma.conf.js ./karma.conf.js
COPY tslint.json ./tslint.json
COPY package.json .
COPY angular.json .
COPY .npmrc .
RUN npm install --verbose
RUN npm build --verbose

I hope this helps someone.

The solution mentioned here works. https://github.com/nodejs/docker-node/issues/437#issuecomment-320993300

In addition, npm added the npx command for running installed dependencies which significantly simplifies running globally installed modules.