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

Heroku部署Discord Bot时执行npm start报错:index.js not found、ELIFECYCLE及ENOENT问题求助

问题:Heroku部署Discord Bot时npm start报错,Bot离线

我正尝试通过Heroku部署一款Discord Bot,但每次执行npm start命令时都会出现npm错误码,且Bot在Discord上始终处于离线状态。我已尝试执行npm cache clean --force、rmdir /S /Q node_modules以及npm install命令,但均未解决问题。

控制台日志

2021-05-09T11:37:13.126499+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-05-09T11_37_13_077Z-debug.log
2021-05-09T11:37:13.225562+00:00 heroku[web.1]: Process exited with status 1
2021-05-09T11:37:13.398582+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-09T11:37:20.322275+00:00 app[api]: Starting process with command `npm start` by user <email redacted>
2021-05-09T11:37:23.274907+00:00 heroku[run.4911]: State changed from starting to up
2021-05-09T11:37:23.427148+00:00 heroku[run.4911]: Awaiting client
2021-05-09T11:37:23.726119+00:00 heroku[run.4911]: Starting process with command `npm start`
2021-05-09T11:37:28.678579+00:00 heroku[run.4911]: Process exited with status 1
2021-05-09T11:37:28.742179+00:00 heroku[run.4911]: State changed from up to complete
2021-05-09T11:44:03.004685+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-09T11:44:05.335454+00:00 heroku[web.1]: Starting process with command `npm start`
2021-05-09T11:44:08.088534+00:00 heroku[web.1]: Process exited with status 1
2021-05-09T11:44:08.229000+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-09T11:44:08.232296+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-09T11:44:08.000901+00:00 app[web.1]: 
2021-05-09T11:44:08.000925+00:00 app[web.1]: > CalculatorBot@1.0.0 start /app
2021-05-09T11:44:08.000925+00:00 app[web.1]: > index.js
2021-05-09T11:44:08.000925+00:00 app[web.1]: 
2021-05-09T11:44:08.005333+00:00 app[web.1]: sh: 1: index.js: not found
2021-05-09T11:44:08.012134+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-05-09T11:44:08.012381+00:00 app[web.1]: npm ERR! syscall spawn
2021-05-09T11:44:08.012474+00:00 app[web.1]: npm ERR! file sh
2021-05-09T11:44:08.012618+00:00 app[web.1]: npm ERR! errno ENOENT
2021-05-09T11:44:08.016105+00:00 app[web.1]: npm ERR! CalculatorBot@1.0.0 start: `index.js`
2021-05-09T11:44:08.016163+00:00 app[web.1]: npm ERR! spawn ENOENT
2021-05-09T11:44:08.016238+00:00 app[web.1]: npm ERR! 
2021-05-09T11:44:08.016285+00:00 app[web.1]: npm ERR! Failed at the CalculatorBot@1.0.0 start script.
2021-05-09T11:44:08.016364+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-05-09T11:44:08.024564+00:00 app[web.1]: 
2021-05-09T11:44:08.024647+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-05-09T11:44:08.024701+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-05-09T11_44_08_016Z-debug.log
2021-05-09T11:44:11.422108+00:00 heroku[web.1]: Starting process with command `npm start`
2021-05-09T11:44:14.301131+00:00 heroku[web.1]: Process exited with status 1
2021-05-09T11:44:14.438297+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-09T11:44:14.212635+00:00 app[web.1]: 
2021-05-09T11:44:14.212651+00:00 app[web.1]: > CalculatorBot@1.0.0 start /app
2021-05-09T11:44:14.212652+00:00 app[web.1]: > index.js
2021-05-09T11:44:14.212652+00:00 app[web.1]: 
2021-05-09T11:44:14.217415+00:00 app[web.1]: sh: 1: index.js: not found
2021-05-09T11:44:14.225315+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-05-09T11:44:14.225593+00:00 app[web.1]: npm ERR! syscall spawn
2021-05-09T11:44:14.225710+00:00 app[web.1]: npm ERR! file sh
2021-05-09T11:44:14.225853+00:00 app[web.1]: npm ERR! errno ENOENT
2021-05-09T11:44:14.229827+00:00 app[web.1]: npm ERR! CalculatorBot@1.0.0 start: `index.js`
2021-05-09T11:44:14.229941+00:00 app[web.1]: npm ERR! spawn ENOENT
2021-05-09T11:44:14.230071+00:00 app[web.1]: npm ERR! 
2021-05-09T11:44:14.230234+00:00 app[web.1]: npm ERR! Failed at the CalculatorBot@1.0.0 start script.
2021-05-09T11:44:14.230359+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-05-09T11:44:14.237148+00:00 app[web.1]: 
2021-05-09T11:44:14.237336+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-05-09T11:44:14.237482+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-05-09T11_44_14_231Z-debug.log

package.json内容

{
  "name": "CalculatorBot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^12.5.3",
    "math": "0.0.3"
  }
}

解决方案

Hey there, let's break down what's going wrong here and fix it step by step.

First, looking at your error logs, the key line is:

sh: 1: index.js: not found

This is happening because your start script in package.json is just index.js—when Heroku runs this, it tries to execute the file directly as a shell script, which it isn't. It doesn't know to use Node.js to run it.

Fix 1: Update the start script

Modify your package.json's scripts section to explicitly use Node to run your entry file:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
}

Adding node before index.js tells the system to use Node.js to execute the script, which is the correct way to run Node projects.

Fix 2: Configure Heroku to run a worker process (not web)

Discord Bots don't need to listen on an HTTP port, but Heroku defaults to treating your app as a web service. If you don't listen on the required port, Heroku will kill your process after a timeout. To fix this:

  1. Create a file named Procfile in your project root (no file extension) with this content:
    worker: npm start
    
  2. Deploy this change to Heroku, then scale down the web process and scale up the worker process:
    • You can do this via Heroku's dashboard: Go to your app's "Resources" tab, turn off the web dyno, and turn on the worker dyno.
    • Or via the command line:
      heroku ps:scale web=0 worker=1
      

Quick check

Make sure your index.js file is in the root directory of your project, at the same level as package.json. If it's in a subfolder, adjust the start script to point to the correct path (e.g., node ./src/index.js).

After making these changes, commit your code and push it to Heroku again. Your Bot should start up without the "file not found" error and come online in Discord.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 16:17:30