我有个运行良好的docker映像,直到必须将新的npm程序包添加到node.js应用程序,然后,新添加的模块“未找到”。我以为这个包装可能不好,所以我尝试了另一个包装以得到相同的结果(未找到)。这些是我的文件:
docker-compose.yml
version: '3.8'
services:
builditjobs:
container_name: 'BIJ-API'
build: .
ports:
- '12100:12100'
depends_on:
- db
volumes:
- './api:/var/apps/BIJ' # Mount the local directory to the image so we don't have to restart our image to get new code
- node_modules:/var/apps/BIJ/node_modules
db:
container_name: 'DB'
image: mysql:5.7
ports:
- '3306:3306'
volumes:
- db-data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=*******
- MYSQL_DATABASE=bij
- MYSQL_USER=bij
- MYSQL_PASSWORD=******
admin:
container_name: 'ADMINER'
image: adminer
depends_on:
- db
ports:
- '8080:8080'
volumes:
db-data:
node_modules:
Dockerfile
FROM node:10
RUN mkdir -p /var/apps/BIJ
WORKDIR /var/apps/BIJ
RUN apt-get update && apt-get install wait-for-it
RUN npm i npm@latest -g
RUN node -v
RUN npm -v
RUN npm install pm2 -g
RUN pm2 ls
COPY package.json ./
RUN npm install --no-optional && npm cache clean -f
RUN chown -R node:node /var/apps/BIJ
RUN ls -la
EXPOSE 12100
ENTRYPOINT ./docker-entrypoint.sh
docker-entrypoint:
#!/bin/bash
wait-for-it db:3306 -- pm2 start ecosystem.config.js && pm2 logs API-1.0.0 --raw
package.json
{
"name": "bij-api",
"version": "1.0.0",
"description": "BIJ API",
"main": "server.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "CD",
"license": "ISC",
"dependencies": {
"@setreflex/logger": "^1.0.10",
"@types/node": "^14.0.11",
"body-parser": "^1.19.0",
"colors": "^1.4.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"crypto-js": "^4.0.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mariadb": "^2.4.0",
"moment": "^2.25.3",
"morgan": "^1.10.0",
"nodemailer": "^6.4.6",
"q": "^1.5.1",
"schema-inspector": "^1.6.9"
}
}
有什么想法吗?我强调:直到我做了一个
npm i crypto-js -s
并重建图像。谢谢!
LE:这是我得到的确切错误:
internal/modules/cjs/loader.js:638
BIJ-API | throw err;
BIJ-API | ^
BIJ-API |
BIJ-API | Error: Cannot find module 'crypto-js'
BIJ-API | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
BIJ-API | at Function.Module._load (internal/modules/cjs/loader.js:562:25)
BIJ-API | at Module.require (internal/modules/cjs/loader.js:692:17)
BIJ-API | at require (internal/modules/cjs/helpers.js:25:18)
答案 0 :(得分:1)
我的建议是在每次构建依赖项时都安装依赖项(node_modules)。
Dockerfile如下...
FROM node:12.3.1
WORKDIR /www/api
RUN npm install pm2 -g
RUN npm install babel-cli -g
ADD package.json /www/api
RUN npm install
ADD . /www/api
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
EXPOSE 3000
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
撰写文件为
时api:
build: .
entrypoint:
- /usr/local/bin/docker-entrypoint.sh
restart: always
ports:
- "3000:3000"`
然后您可以添加.dockerignore来忽略节点模块