You need to enable JavaScript to run this app. 在Heroku运行Discord机器人报node_modules已提交源码控制错误如何修复
优惠活动
大模型
产品
解决方案
定价
更多

在Heroku运行Discord机器人报node_modules已提交源码控制错误如何修复

问题现象

Heroku平台部署Node.js编写的Discord机器人时,构建流程持续失败,服务无法启动。用户将项目文件连同本地node_modules依赖目录一并推送到GitHub仓库后,Heroku返回的核心构建报错如下:

-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       NODE_VERBOSE=false
       
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       
       Resolving node version 12.x...
       Downloading and installing node 12.19.1...
       Using default npm version: 6.14.8
       
-----> Installing dependencies
       Prebuild detected (node_modules already exists)
       Rebuilding any native modules
       
       > @discordjs/opus@0.3.2 install /tmp/build_8234188f_/node_modules/@discordjs/opus
       > node-pre-gyp install --fallback-to-build
       
       sh: 1: node-pre-gyp: Permission denied
       npm ERR! code ELIFECYCLE
       npm ERR! errno 126
       npm ERR! @discordjs/opus@0.3.2 install: `node-pre-gyp install --fallback-to-build`
       npm ERR! Exit status 126

Heroku日志已直接定位核心问题:node_modules 目录被纳入了Git版本控制。

修复操作步骤
  • 从Git追踪列表中移除已提交的node_modules
    执行命令,仅取消Git对该目录的版本追踪,不删除本地实际文件:
    git rm -r --cached node_modules
  • 配置.gitignore忽略规则
    在项目根目录的.gitignore文件中新增一行 node_modules/,避免后续提交时再次上传依赖目录
  • 明确指定Node.js运行版本
    在package.json顶层新增engines配置,避免Heroku默认拉取已停止维护的Node 12版本:
    "engines": {
      "node": "18.x",
      "npm": "9.x"
    }
    
  • 提交改动触发重新部署
    将上述所有修改推送到远程仓库,Heroku会在干净的Linux构建环境中自动重新安装全部依赖,彻底解决本地编译模块权限不匹配的报错。
原理说明

本地提交的node_modules无法跨环境兼容:本地操作系统下编译生成的@discordjs/opus等原生模块,二进制格式、文件权限与Heroku的Linux运行环境完全不匹配,即使解决了当前的权限报错,部署后也无法正常调用语音功能。Heroku的Node.js构建流程原生支持依赖自动安装,无需手动上传node_modules目录。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.15 23:36:04