我试图在站点文件夹外的模块中要求pg
,但我得到以下错误:
> node app.js
/deploy/ui/site
node:internal/modules/cjs/loader:1078
throw err;
^
Error: Cannot find module 'pg'
Require stack:
- /deploy/lib/db.js
- /deploy/ui/site/app.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
at Module._load (node:internal/modules/cjs/loader:920:27)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Object.<anonymous> (/deploy/lib/db.js:3:18)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/deploy/lib/db.js', '/deploy/ui/site/app.js' ]
}
我有以下文件和文件夹
/lib/db.js
const { Pool } = require('pg')
/ui/site/app.js
const db = require('../../lib/db')
我在VS Code中成功地在本地运行了它,但在Docker中运行时,我得到了错误。我也尝试过全局安装pg
npm install -g pg
验证是否安装了
/ # npm ls -g --depth=0
/usr/local/lib
+-- corepack@0.17.0
+-- npm@9.5.1
`-- pg@8.10.0
不幸的是,同样的问题。
Dockerfile
# Use the official Node.js runtime as the base image
FROM node:18-alpine
# Set env params
ENV RENDERTYPE=il2
ENV GAMEPATH="G:\\Spel\\"
COPY lib /deploy/lib
COPY mission /deploy/mission
# node_modules excluded in .dockerignore
COPY ui/site /deploy/ui/site
# COPY package*.json ./
RUN npm install -g pg@8.8.0
# Create and set the working directory
WORKDIR /deploy/ui/site
# Copy the package.json and package-lock.json files to the working directory
# COPY ui/site/package*.json ./
# Install dependencies
RUN npm install --omit=dev --loglevel verbose
# Expose port 3000 for the application
EXPOSE 3000
# Start the application
# CMD ["node", "app.js"]
CMD ["npm", "start"] # run a temporary http server for testing
1条答案
按热度按时间c0vxltue1#
好吧,我知道问题出在哪里了。似乎我在某个时候开始使用可重用代码,为网站奠定了基础,我还为此设置了
package.json
。我用下面的代码更新了Dockerfile
以安装依赖项