Home > Mobile >  Docker npm install not working, how can I solve?
Docker npm install not working, how can I solve?

Time:10-21

I extend my context because I need some file outside of Dockerfile directory.

This is my command : docker build -f Dockerfile ../../ docker run (imageID)

file structure:

  • model
  • servers
    • subscribeship (I run docker command in this directory)
      • Dockerfile
      • index.js
      • package.json
      • package-lock.json

in my Dockerfile:

FROM node:10

WORKDIR /app

COPY ./servers/subscribeship/package*.json .

RUN npm install

COPY ./servers/subscribeship .

COPY ./models ../../models

EXPOSE 3000

CMD ["node","index.js"]

log for build image:

 ] Building 7.5s (11/11) FINISHED                                                                                                                         
 => [internal] load build definition from Dockerfile                                                                                                  0.0s
 => => transferring dockerfile: 231B                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                     0.0s
 => => transferring context: 34B                                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/node:10                                                                                            2.7s
 => [1/6] FROM docker.io/library/node:10@sha256:59531d2835edd5161c8f9512f9e095b1836f7a1fcb0ab73e005ec46047384911                                      0.0s
 => [internal] load build context                                                                                                                     1.3s
 => => transferring context: 23.27MB                                                                                                                  1.3s
 => CACHED [2/6] WORKDIR /app                                                                                                                         0.0s
 => [3/6] COPY ./servers/subscribeship/package*.json .                                                                                                0.1s
 => [4/6] RUN npm install                                                                                                                             2.6s
 => [5/6] COPY ./servers/subscribeship .                                                                                                              0.4s
 => [6/6] COPY ./models ../../models                                                                                                                  0.0s 
 => exporting to image                                                                                                                                0.3s 
 => => exporting layers                                                                                                                               0.3s 
 => => writing image sha256:9a87c205096b94e442a9f40e7c050ac68383f2da7fd8b285b5ee840c20f922af  

log for run container:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'sequelize'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/models/index.js:5:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

It seems like npm install not working. package.json & package-lock.json indeed have sequelize package

enter image description here

I use same method for test if is my way wrong but it seems work..

I create another directory call bug, as same as servers level. and then create bug1 directory inside bug folder like this:

command:

docker build -f Dockerfile ../../
docker run (imageId)
  • bug
    • bug1 (run command in here)
      • Dockerfile
      • package.json
      • pakcage-lock.json
      • .dockerignore

dockerfile:

FROM node:10

WORKDIR /app

COPY ./bug/bug1/package.json .

RUN npm install

COPY ./bug/bug1 .

EXPOSE 3000

CMD ["node","index.js"]

enter image description here

CodePudding user response:

./ means you are navigating from the current directory. If you are running a command in /servers/subscribeship, ./ will refer to this directory. ./servers/subscribeship/package*.json will then refer to /servers/subscribeship/servers/subscribeship/package*.json. Because this directory does not exist package.json is not copied and npm install will not install the required modules.

CodePudding user response:

I solved my problem. It turn out that model directory doesn't have package.json.

  • Related