Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent npm from creating the ~/.npm directory when I run "npm install"?

Tags:

npm

docker

I have a Vue.js project that I am attempting build in Docker using the node:latest container. Here are the relevant lines in my Makefile that installs the packages the project requires:

CWD = $(shell pwd)
UID = $(shell id -u)
GID = $(shell id -g)

# ...

sometarget:
    docker run \
        --rm \
        -e HOME=/home/node \
        -u ${UID}:${GID} \
        -v ${CWD}:/usr/src/app \
        -w /usr/src/ui \
        node:latest \
        npm install

Note: by overriding user and group IDs, I am able to ensure that the files created by npm install have the correct ownership, allowing the current user to run make clean afterwards.

The npm install invocation attempts to create ~/.npm. However, if the current user and group ID do not match those of the node user in the container, I end up with an error:

npm ERR! path /home/node/.npm
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir
npm ERR! Error: EACCES: permission denied, mkdir '/home/node/.npm'

I'd like to have npm skip the creation of /home/node/.npm entirely, if possible. Is there a flag that prevents the creation of this directory? The container is ephemeral, so there is no value in preserving the contents of the directory.

like image 808
Nathan Osman Avatar asked Feb 16 '18 01:02

Nathan Osman


People also ask

When install node package it gets created in the folder called?

Now you have downloaded and installed your first package! NPM creates a folder named "node_modules", where the package will be placed.

Where does npm install packages by default?

On windows I used npm list -g to find it out. By default my (global) packages were being installed to C:\Users\[Username]\AppData\Roaming\npm . Show activity on this post.

What is npm clean install?

The npm clean-install command (or npm ci for short) is an in-place replacement for npm install with two major differences: It does a clean install: if the node_modules folder exists, npm deletes it and installs a fresh one. It checks for consistency: if package-lock.


1 Answers

From the github issue:

The short answer is no, it's not possible to bypass the CLI's cache.

Basically, npm install is essentially:

  1. populate the cache with the requested packages
  2. extract from the cache

Given that, your options likely change to:

  1. (as the node user) npm install && npm cache clean
  2. chown -R "$UID:$GID" node_modules

Another option is to mount a temp directory as the user (assume TMP_DIR was made using mktmp -d or some sort):

    -v $TMP_DIR:/home/node \
    -e HOME=/home/node \

The mounted directory will exist and will be owned by the user so it'll be writable.

Yet another option (since you don't actually care about the cache directory at all) is to set HOME to the temporary directory (which has the sticky bit set so it'll be writable by anyone:

    -e HOME=/tmp \
like image 156
Anthony Sottile Avatar answered Sep 23 '22 01:09

Anthony Sottile