Home > other >  Why is npm install not working inside docker?
Why is npm install not working inside docker?

Time:11-29

I own an Asustor and I want to dockerise my node js app to be able to run any time. I followed official tutorial from nodejs but it's not working as intended on my nas, while on my computer everything is fine.

Here is my Dockerfile :

FROM node:latest
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["node", "app.js"]

And here is the error I'm getting on the nas :

Sending build context to Docker daemon  45.57kB
Step 1/7 : FROM node:latest
 ---> b254e440661a
Step 2/7 : WORKDIR /app
 ---> Running in 4ddb713a2c92
Removing intermediate container 4ddb713a2c92
 ---> 7956afe6d600
Step 3/7 : COPY package*.json ./
 ---> 9a814bfae80d
Step 4/7 : RUN npm install
 ---> Running in 477d3abd6312
node:internal/fs/utils:347
    throw err;
    ^

Error: ENOENT: no such file or directory, open '/usr/local/lib/cli.js'
    at Object.openSync (node:fs:591:3)
    at Object.readFileSync (node:fs:459:35)
    at Module._extensions..js (node:internal/modules/cjs/loader:1222:18)
    at Module.load (node:internal/modules/cjs/loader:1068:32)
    at Module._load (node:internal/modules/cjs/loader:909:12)
    at Module.require (node:internal/modules/cjs/loader:1092:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/usr/local/bin/npm:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1205:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1259:10) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/usr/local/lib/cli.js'
}

Node.js v19.1.0
The command '/bin/sh -c npm install' returned a non-zero code: 1

CodePudding user response:

From the official guide you've sent, you can clearly see in the following link, I highlighted the version they used in the main guide and in your Dockerfile your using FROM node:latest meanwhile they are using node:10 can you please edit your Dockerfile and change the latest to 10 in order to fix node version to the

CodePudding user response:

Sorry, cannot reproduce!

Sticking to example/combining with your Dockerfile:

  1. mkdir test-project && cd test-project.

  2. package.json:

    {
      "name": "docker_web_app",
      "version": "1.0.0",
      "description": "Node.js on Docker",
      "author": "First Last <[email protected]>",
      "main": "server.js",
      "scripts": {
        "start": "node server.js"
      },
      "dependencies": {
        "express": "^4.16.1"
      }
    }
    
  3. server.js (according to package.json, not app.js!):

    'use strict';
    
    const express = require('express');
    
    // Constants
    const PORT = 8080;
    const HOST = '0.0.0.0';
    
    // App
    const app = express();
    app.get('/', (req, res) => {
      res.send('Hello World');
    });
    
    app.listen(PORT, HOST, () => {
      console.log(`Running on http://${HOST}:${PORT}`);
    });
    
  4. Dockerfile (basically yours, subtle difference: server.js vs. app.js):

    FROM node:latest
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    EXPOSE 8080
    CMD ["node", "server.js"]
    
  5. (.dockerignore:

    node_modules
    npm-debug.log
    

    , but also without trying npm locally)

  6. docker build . -t bruce/node-docker-demo (takes a few minutes, but succeeds).

  7. Verify with docker images.

  8. Run with docker run -p 49160:8080 -d bruce/node-docker-demo.

  9. Get Container ID via docker ps.

  10. Get logs via docker logs <container_id>:

    Running on http://0.0.0.0:8080
    
  11. Finally test with curl -i localhost:49160.

    HTTP/1.1 200 OK
    X-Powered-By: Express
    Content-Type: text/html; charset=utf-8
    Content-Length: 11
    ETag: W/"b-Ck1VqNd45QIvq3AZd8XYQLvEhtA"
    Date: Mon, 28 Nov 2022 18:05:26 GMT
    Connection: keep-alive
    Keep-Alive: timeout=5
    
    Hello World
    
  • Related