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

Heroku部署Node.js项目报错:安装18.x版本Node时出现未知错误

Heroku构建Node.js项目报错:未知错误安装Node 18.x

问题描述

项目此前构建成功,当前构建失败,报错如下:

Error: Unknown error installing "18.x" of node

已尝试回滚package.json和package-lock.json文件,问题仍未解决。完整构建日志:

-----> Building on the Heroku-22 stack
-----> Using buildpack: heroku/nodejs
-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       
       Resolving node version 18.x...
       Error: Unknown error installing "18.x" of node
-----> Build failed
       
       We're sorry this build is failing! You can troubleshoot common issues here:
       https://devcenter.heroku.com/articles/troubleshooting-node-deploys
       
       Some possible problems:
       
       - Node version not specified in package.json
         https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
       
       Love,
       Heroku
       
 !     Push rejected, failed to compile Node.js app.
 !     Push failed

解决方案

  • 明确指定Node和npm版本:在package.json的engines字段中添加具体的版本号,避免Heroku自动解析版本时出现异常。示例:

    "engines": {
      "node": "18.17.1",
      "npm": "9.6.7"
    }
    

    选择Heroku官方支持的Node 18.x系列稳定版本,确保兼容性。

  • 清除Heroku构建缓存:缓存可能导致版本解析异常,运行以下命令清理缓存后重新构建:

    heroku builds:cache:purge
    
  • 确认Stack兼容性:当前使用的Heroku-22栈支持Node 16及以上版本,无需切换栈,但指定具体版本能消除自动解析的不确定性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 10:20:25