mongoose 部署节点时出错,js app www.example.com

tct7dpnv  于 2023-04-30  发布在  Go
关注(0)|答案(1)|浏览(323)

首次部署到 www.example.com

我尝试使用fly deploy进行部署,但失败了,所以我遵循了本教程https://youtu.be/uoJ0Tv-BFcQ?list=LL&t=18827,我尝试使用Docker本地使用fly deploy --local-only命令进行部署,但我得到了以下错误:

=> ERROR [build 3/5] RUN npm install --production=false                                   3.3s 
------
 > [build 3/5] RUN npm install --production=false:
#10 0.938 npm WARN config production Use `--omit=dev` instead.
#10 3.216 npm ERR! code ERESOLVE
#10 3.226 npm ERR! ERESOLVE could not resolve
#10 3.226 npm ERR!
#10 3.227 npm ERR! While resolving: mongoose-currency@0.2.0
#10 3.227 npm ERR! Found: mongoose@7.0.3
#10 3.228 npm ERR! node_modules/mongoose
#10 3.229 npm ERR!   mongoose@"^7.0.3" from the root project
#10 3.229 npm ERR!
#10 3.230 npm ERR! Could not resolve dependency:
#10 3.231 npm ERR! peer mongoose@"~> 4.x" from mongoose-currency@0.2.0
#10 3.231 npm ERR! node_modules/mongoose-currency
#10 3.232 npm ERR!   mongoose-currency@"^0.2.0" from the root project
#10 3.233 npm ERR!
#10 3.233 npm ERR! Conflicting peer dependency: mongoose@4.13.21
#10 3.233 npm ERR! node_modules/mongoose
#10 3.234 npm ERR!   peer mongoose@"~> 4.x" from mongoose-currency@0.2.0
#10 3.234 npm ERR!   node_modules/mongoose-currency
#10 3.235 npm ERR!     mongoose-currency@"^0.2.0" from the root project
#10 3.236 npm ERR!
#10 3.237 npm ERR! Fix the upstream dependency conflict, or retry
#10 3.238 npm ERR! this command with --force or --legacy-peer-deps
#10 3.238 npm ERR! to accept an incorrect (and potentially broken) dependency resolution.       
#10 3.238 npm ERR!
#10 3.238 npm ERR!
#10 3.238 npm ERR! For a full report see:
#10 3.238 npm ERR! /root/.npm/_logs/2023-04-28T09_06_28_271Z-eresolve-report.txt
#10 3.242
#10 3.242 npm ERR! A complete log of this run can be found in:
#10 3.242 npm ERR!     /root/.npm/_logs/2023-04-28T09_06_28_271Z-debug-0.log
------
Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c 
npm install --production=false]: exit code: 1

看起来这和 Mongoose 没有关系,但我不确定到底发生了什么。这是我的Dockerfile:

# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=18.15.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="NodeJS"

# NodeJS app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV=production

# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
    apt-get install -y python-is-python3 pkg-config build-essential 

# Install node modules
COPY --link package.json package-lock.json .
RUN npm install --production=false

# Copy application code
COPY --link . .

# Remove development dependencies
RUN npm prune --production

# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
CMD [ "npm", "run", "start" ]

这是我的包裹json:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "helmet": "^6.1.2",
    "mongoose": "^7.0.3",
    "mongoose-currency": "^0.2.0",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.22"
  }
}
tvokkenx

tvokkenx1#

经过24小时的调试,我终于解决了这个问题。主要的问题是mongoose-currency需要mongoose verion 4,这就是为什么会发生依赖冲突。当我试图在终端本地运行npm install时,由于依赖项冲突而失败,但当我在终端本地运行npm install --legacy-peer-deps命令时,它工作正常。这里的问题是,当我运行flyctl launch命令时生成的Docker文件不支持--legacy-peer-deps选项,并且已经被www. example之一处理掉了 www.example.com 员工在GitHub上打开并发布https://github.com/fly-apps/dockerfile-node/issues/6
为了避免这个问题,我更新了Dockerfile如下:

# syntax = docker/dockerfile:1

ARG NODE_VERSION=18.15.0
FROM node:$NODE_VERSION-slim as base

LABEL fly_launch_runtime="NodeJS"

# NodeJS app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV=production

# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
    apt-get install -y python-is-python3 pkg-config build-essential 

COPY <<-"EOF" /app/package.json
{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "helmet": "^6.0.1",
    "mongoose": "^7.0.0",
    "mongoose-currency": "^0.2.0",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}
EOF

RUN npm install --legacy-peer-deps

# Copy application code
COPY --link . .

# Remove development dependencies
RUN npm prune --production --legacy-peer-deps

# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
CMD [ "npm", "run", "start" ]

还有包裹json没有包含“start”脚本,这在部署时导致了另一个问题。为了解决这个问题,我更新了软件包。json为以下内容:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index.js",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "helmet": "^6.0.1",
    "mongoose": "^7.0.0",
    "mongoose-currency": "^0.2.0",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}

最后,应用程序被部署并正常运行。我在这里发布了解决方案,以防将来任何人遇到同样的问题或类似的问题。

相关问题