Heroku部署Discord机器人失败,遇node-pre-gyp权限拒绝错误求助
Let's walk through resolving this deployment issue step by step—those two warnings from Heroku are exactly the starting points we need to address:
1. Remove node_modules from Git Version Control
Heroku is right to flag this: your local node_modules folder has system-specific files and permissions that don't translate to Heroku's environment, which is directly causing the node-pre-gyp: Permission denied error. Here's how to fix it:
- First, make sure you have a
.gitignorefile in your project root. If you don't, create one and add this line:node_modules/ - Next, remove the existing
node_modulesfrom Git's cached files (this won't delete the folder locally):git rm -r --cached node_modules - Commit this change to your repo:
git commit -m "Remove node_modules from version control"
2. Specify Node & npm Versions in package.json
Heroku is falling back to a default Node 14.x version, but not locking in versions can lead to compatibility issues with packages like @discordjs/opus. Let's set versions that work for your bot:
- Open your
package.jsonfile and add anenginessection. Use the same Node version you're running locally (check withnode -vin your terminal) or a stable LTS version. For example:"engines": { "node": "16.x", "npm": "8.x" } - Commit this update:
git commit -m "Add Node and npm version specifications"
3. Redeploy to Heroku
Now push your updated code to Heroku:
git push heroku main
This should resolve the permission error and let Heroku properly install your dependencies in its own environment. The @discordjs/opus package will build correctly once Heroku handles the dependency installation from scratch, without relying on your local node_modules files.
内容的提问来源于stack exchange,提问作者Rhovy

