无法在生产服务器中的 Docker 容器上模块 'reflect-metadata'

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

我收到一个错误

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 运行ning docker-compose 在生产服务器上。虽然我在本地机器上成功了运行,但是我真的很难理解为什么生产服务器会出现这个错误?

我的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" }, ... }

我的 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  

我的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
...

根据您的依赖项列表,我的猜测是 reflect-metadata 被列为 'devDependencies' 而不是常规依赖项。确保它列在 package.json 中的正确部分。 运行 在生产中需要的任何东西,你都需要在 dependencies 部分。

我昨天遇到了同样的问题,并找到了解决方案:

您必须明确地为您的 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
...

为了确保我在服务器上的应用程序在尝试之前是干净的,我还删除了 Docker 图像并重建了所有内容