简体   繁体   中英

Can't module 'reflect-metadata' on Docker container in Production Server

I get an error

module.js:550
     throw err;
     ^

 Error: Cannot find module 'reflect-metadata'
     at Function.Module._resolveFilename (module.js:548:15)
     at Function.Module._load (module.js:475:25)
     at Module.require (module.js:597:17)
     at require (internal/module.js:11:18)
     at Object.<anonymous> (/usr/src/app/dist/App.js:11:1)
     at Module._compile (module.js:653:30)
     at Object.Module._extensions..js (module.js:664:10)
     at Module.load (module.js:566:32)
     at tryModuleLoad (module.js:506:12)
     at Function.Module._load (module.js:498:3)

when running docker-compose on the production server. Although I have successfully run it on a local machine, I really find it difficult to understand why this error occurred in the production server?

My package.json:
{ ... "dependencies": { "@types/mocha": "2.2.41", "@types/node": "7.0.22", "@types/express": "^4.16.0", ... "nodemon": "latest", "puppeteer": "latest", "reflect-metadata": "latest", "typeorm": "0.2.16", "typescript": "latest" }, ... }

My Dockerfile:

FROM node:8.15.0
MAINTAINER Nguyen Tien-Linh <nguyentienlinh2611@gmail.com>

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN yarn install

COPY . /usr/src/app

COPY wait-for-it.sh /

EXPOSE 3000

CMD /wait-for-it.sh database:3306 -- yarn run prod  

My docker-compose.yml:

...
   web:
      container_name: vnuonline_web
      privileged: true
      build: .
      ports:
         - "3000:3000"
      volumes:
         - data:/usr/src/app/data
         - .:/usr/src/app
         - /var/run/docker.sock:/var/run/docker.sock
      restart: on-failure
      links:
         - openface
         - database
      depends_on:
         - openface
         - database
...

Based on your dependency list my guess is that reflect-metadata is listed as a 'devDependencies' and not a regular dependency. Make sure it is listed in the correct section in package.json . Anything you need for running in production, you need in the dependencies section.

I had the same issue yesterday, and found a solution :

You have to explicitly define a volume for your node_modules :

...
   web:
      container_name: vnuonline_web
      privileged: true
      build: .
      ports:
         - "3000:3000"
      volumes:
         - data:/usr/src/app/data
         - .:/usr/src/app
         - /var/run/docker.sock:/var/run/docker.sock
         - /usr/src/app/node_modules    # new line here
      restart: on-failure
      links:
         - openface
         - database
      depends_on:
         - openface
         - database
...

To make sure my app on server was clean before trying, I also removed the Docker images and rebuilt everything

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