简体   繁体   中英

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

Hello sir i am new to docker, i am using ubuntu budgie(linux) 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 have install nodejs and npm in my host machine form this link nodejs install link 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.

I had the same problem and i tried several solution but the only one how did work is to change docker version

docker-ce|5:18.09.0~3-0~ubuntu-bionic|https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
docker-ce|18.06.3~ce~3-0~ubuntu|https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
docker-ce|18.06.2~ce~3-0~ubuntu|https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages

i was using 5:19.03.9~3-0~ubuntu-bionic and i switched to 18.06.3~ce~3-0~ubuntu

I kept the same Dockerfile config:

FROM node:10 
USER root 
WORKDIR /home/node/app 
COPY package.json . 
RUN npm install 

# Bundle app source COPY . /app

EXPOSE 8080  CMD [ "node", "server.js" ]

it returns:

Step 5/8 : RUN npm install
---> Running in 5b6cd0a9a7cd
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated

> nodemon@1.19.4 postinstall /home/node/app/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN quiz-service@1.0.0 No description
npm WARN quiz-service@1.0.0 No repository field.

added 322 packages from 227 contributors and audited 323 packages in 21.573s

1 package is looking for funding
run `npm fund` for details

found 0 vulnerabilities

Removing intermediate container 5b6cd0a9a7cd
---> cc344027e126
Step 6/8 : COPY . /app
---> 10841d9cf0bf
Step 7/8 : EXPOSE 8080

you can check this link for more detail on reinstalling docker

This error originates from the fact, that your current user does not have enough permission to access the npm cli file.

Did you try the minimal Dockerfile:

FROM node:lts

WORKDIR /home/node
COPY . .
RUN npm install

CMD ['npm', 'start']

Does it work for you? If so, the problem in your Dockerfile, I assume in user node or you have some other script we don't see in OP.

Please try to use yarn instead of node.

FROM node:10 
USER root 
WORKDIR /home/node/app 
COPY package.json . 
RUN yarn install 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM