You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次部署Node.js应用至Fly.io遇依赖冲突错误求助

问题分析与解决方法

问题根源

mongoose-currency@0.2.0 是一个长期未维护的包(最后更新于2017年),它的peer依赖明确要求 mongoose~>4.x,但你的项目使用的是 mongoose@7.0.3,两者版本差距过大。npm从v7开始默认严格校验peer依赖,直接导致依赖解析失败。

解决方法

方案1:替换未维护的mongoose-currency(推荐)

由于mongoose-currency多年未更新,无法适配新版mongoose,建议替换为自定义货币类型或维护活跃的替代方案:

  • 卸载旧包:
    npm uninstall mongoose-currency
    
  • 自定义mongoose货币类型(示例):
    在代码中添加自定义Schema类型替代原包功能:
    import mongoose from 'mongoose';
    
    const Currency = mongoose.SchemaType.extend('Currency', {
      cast(val) {
        const num = Number(val);
        return isNaN(num) ? 0 : Math.round(num * 100) / 100; // 保留两位小数
      }
    });
    
    // 使用示例
    const productSchema = new mongoose.Schema({
      price: { type: Currency, required: true }
    });
    

方案2:临时绕过peer依赖检查

如果需要快速部署测试,可修改Dockerfile中的npm install命令,添加--legacy-peer-deps参数(兼容旧版peer依赖解析逻辑):
修改Dockerfile中build阶段的安装命令:

RUN npm install --production=false --legacy-peer-deps

注意:此方法仅为临时解决,可能引入潜在兼容性问题,不建议长期使用。

方案3:降级mongoose到4.x系列(不推荐)

将mongoose版本降级到~4.13.21以满足peer依赖要求,但该版本存在大量安全漏洞,且与现代Node.js版本可能存在兼容性问题,仅作为最后备选:

  • 修改package.json中的mongoose版本:
    "mongoose": "~4.13.21"
    
  • 删除package-lock.json后重新安装依赖

错误日志

=> 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

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" ]

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.1.2",
    "mongoose": "^7.0.3",
    "mongoose-currency": "^0.2.0",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.22"
  }
}

内容的提问来源于stack exchange,提问作者Kareem Abbas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 19:14:56