Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EACCES: permission denied, open '/usr/local/lib/node_modules/npm/bin/npm-cli.js' #1262

Closed
binoysarker opened this issue May 24, 2020 · 8 comments

Comments

@binoysarker
Copy link

Hello sir i am new to docker, i am using ubuntu budgie 20.04 my docker version is
Docker version 18.09.9, build 1752eb3
i have install docker using snap package manager and the path is like this
/snap/bin/docker.machine /snap/bin/docker.compose /snap/bin/docker /snap/bin/docker.help
I am learning docker by making a single nodejs express app with docker. Here is my Dockerfile

FROM node:lts
USER node
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH=$PATH:/home/node/.npm-global/bin
WORKDIR /home/node
COPY package.json .
RUN npm install
COPY . .
CMD ['npm', 'start']

here is my app.js file

const express = require('express');
const app = express()

app.get('/', (req, res) => {
  res.send('hello world')
})

app.listen(3000, () => {
  console.log('app is renning at 3000');
})

When i start to build an image from this dockerfile i get this error

internal/fs/utils.js:230
    throw err;
    ^

Error: EACCES: permission denied, open '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
    at Object.openSync (fs.js:458:3)
    at Object.readFileSync (fs.js:360:35)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1152:22)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  errno: -13,
  syscall: 'open',
  code: 'EACCES',
  path: '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
}
The command '/bin/sh -c npm install --only=prod' returned a non-zero code: 1

Please tell me how can i fix this.
Thanks.

@dmnk89
Copy link

dmnk89 commented May 25, 2020

I am also new to docker and I have similar problem
trying to follow https://medium.com/@wkrzywiec/build-and-run-angular-application-in-a-docker-container-b65dbbc50be8

with Dockerfile:

FROM node:12.7-alpine AS build
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install

i get

Sending build context to Docker daemon  1.758MB
Step 1/4 : FROM node:12.7-alpine AS build
 ---> d97a436daee9
Step 2/4 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 0050b13e417b
Step 3/4 : COPY package.json ./
 ---> Using cache
 ---> 7fc1bf14ab9e
Step 4/4 : RUN npm install
 ---> Running in 8a5516ae6a57
fs.js:126
    throw err;
    ^

Error: EACCES: permission denied, open '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
    at Object.openSync (fs.js:447:3)
    at Object.readFileSync (fs.js:349:35)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:22)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
    at internal/main/run_main_module.js:17:11 {
  errno: -13,
  syscall: 'open',
  code: 'EACCES',
  path: '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
}
The command '/bin/sh -c npm install' returned a non-zero code: 1

and also there is this issue:

$ docker run node:12.7-alpine
/bin/sh: can't open '/usr/local/bin/docker-entrypoint.sh': Permission denied

@LaurentGoderre
Copy link
Member

I just tested this locally and it doesn't seem to be a problem with the image itself.

Running docker run --rm -it -u node -e NPM_CONFIG_PREFIX=/home/node/.npm-global node:lts npm works fine. Can you run ls -lah /home/node and paste the result here?

@dbolan
Copy link

dbolan commented May 26, 2020

That sadly didn't work for me. I'm sure I must be doing something wrong here, since it seems weird that I can't access any of the files at all...

I'm using the same build as binoysarker.

> docker -v
Docker version 18.09.9, build 1752eb3
> docker run --rm -it --entrypoint /bin/sh -u node -e NPM_CONFIG_PREFIX=/home/node/.npm-global node:lts
$ cd ~
$ ls -lah
total 20K
drwxr-xr-x 2 node node 4.0K May 15 20:31 .
drwxr-xr-x 1 root root 4.0K May 15 20:31 ..
-rw-r--r-- 1 node node  220 May 15  2017 .bash_logout
-rw-r--r-- 1 node node 3.5K May 15  2017 .bashrc
-rw-r--r-- 1 node node  675 May 15  2017 .profile
$ cat .bashrc
cat: .bashrc: Permission denied
$ whoami
node
$ npm help
internal/fs/utils.js:230
    throw err;
    ^

Error: EACCES: permission denied, open '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
    at Object.openSync (fs.js:458:3)
    at Object.readFileSync (fs.js:360:35)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1152:22)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  errno: -13,
  syscall: 'open',
  code: 'EACCES',
  path: '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
}
$ 

@LaurentGoderre
Copy link
Member

Did you follow that step when installing docker? https://stackoverflow.com/a/55255557

@dbolan
Copy link

dbolan commented May 26, 2020

I had just been running sudo setfacl -m user:$USER:rw /var/run/docker.sock, but I ran those instructions just to check. Same issue.

I suspected it might have something to do with installing via snap, so I purged the installation and reinstalled using the main ubuntu instructions. Now running Docker version 19.03.9, build 9d988398e7 and everything seems to be working fine.

Still not sure why things had been working for months until just now, but I'm at least up and running again. Thanks for your patience Laurent!

@dmnk89
Copy link

dmnk89 commented May 28, 2020

same
uninstalling snap and installing from mentioned instructions made my setup work

@LaurentGoderre
Copy link
Member

@binoysarker can you try the suggested fix?

@alexbs
Copy link

alexbs commented Aug 20, 2020

@binoysarker in the Dockerfile, all "RUN" commands (listed after "USER node") will be executed on behalf of a "node" user.

  • This is good.
    But other commands (like "COPY") will be still executed on behalf "root" user.

So at first, i recommend to change all you "COPY" commands in this way:

COPY --chown=node:node package.json .
RUN npm install
COPY --chown=node:node . .

@nschonni nschonni closed this as completed Nov 5, 2020
Cruikshanks added a commit to DEFRA/sroc-charging-module-api that referenced this issue Feb 19, 2021
The first builds on docker hub all failed. The error we were getting was

```
[91m ERR! Error: EACCES: permission denied, open '/home/node/package.json'
npm ERR! [Error: EACCES: permission denied, open '/home/node/package.json'] {
npm ERR!�[0m
[91m errno: -13,
npm ERR! code: 'EACCES',
npm �[0m
[91mERR! syscall: 'open',
npm ERR! path: '/home/node/package.json'

...

�[91m 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!�[0m
�[91m
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.
�[0m
�[91m
�[0m
�[91mnpm ERR! A complete log of this run can be found in:
npm ERR!�[0m
�[91m /home/node/.npm/_logs/2021-02-18T09_51_29_644Z-debug.log
�[0m
Removing intermediate container 04acee56b734
The command '/bin/sh -c npm install' returned a non-zero code: 243
```

Some searching of the net indicated that the problem is because the `node` user that is provided in the base image and which we set as the main user does not own the `package.json` files we copy into the image.

It's best practise not to leave the user as `root` in a Docker image, nor to run `npm install` as `root`.

We now understand the reason the `node` user is not the owner of the files is because all `COPY` commands in a Dockerfile are carried out as the `root` user. The suggested fix is to include [chown](https://en.wikipedia.org/wiki/Chown) within your copy statement to ensure once the files are available the non-root user is made the owner.

- nodejs/docker-node#1262 (comment)
- https://stackoverflow.com/a/44766666/6117745
henrotaym added a commit to henrotaym/nuxt-typescript-module-boilerplate that referenced this issue Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants