如何修复Heroku部署错误 – 错误:找不到模块 ‘/app/app.js’

huangapple go评论76阅读模式
英文:

How to fix Heroku Deploy Error - Error: Cannot find module '/app/app.js'

问题

以下是翻译好的部分:

> Error: Cannot find module '/app/app.js'

我能够通过在 package.json 文件的 scripts 块中添加 start 行等详细信息来修复类似的错误。我还尝试将 app.js 文件移动到根目录,更新HTML代码中的引用,然后再次提交和部署。

终端中显示构建成功。然而,当我在Heroku网站上点击“打开应用程序”时,应用程序失败,日志显示如下。有没有人收到过这个错误?如果有的话,你是如何解决的?

Heroku日志:

2023-06-25T09:16:43.627247+00:00 heroku[web.1]: State changed from starting to crashed
2023-06-25T09:16:43.632314+00:00 heroku[web.1]: State changed from crashed to starting
2023-06-25T09:16:45.545297+00:00 heroku[web.1]: Starting process with command `npm start`
2023-06-25T09:16:46.987365+00:00 app[web.1]: 
2023-06-25T09:16:46.987385+00:00 app[web.1]: > herokuapp1@1.0.0 start
2023-06-25T09:16:46.987385+00:00 app[web.1]: > node app.js
2023-06-25T09:16:46.987386+00:00 app[web.1]: 
2023-06-25T09:16:47.019524+00:00 app[web.1]: node:internal/modules/cjs/loader:1080
2023-06-25T09:16:47.019526+00:00 app[web.1]: throw err;
2023-06-25T09:16:47.019526+00:00 app[web.1]: ^
2023-06-25T09:16:47.019526+00:00 app[web.1]: 
2023-06-25T09:16:47.019526+00:00 app[web.1]: Error: Cannot find module '/app/app.js'
2023-06-25T09:16:47.019527+00:00 app[web.1]: at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
2023-06-25T09:16:47.019527+00:00 app[web.1]: at Module._load (node:internal/modules/cjs/loader:922:27)
2023-06-25T09:16:47.019528+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
2023-06-25T09:16:47.019528+00:00 app[web.1]: at node:internal/main/run_main_module:23:47 {
2023-06-25T09:16:47.019528+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2023-06-25T09:16:47.019529+00:00 app[web.1]: requireStack: []
2023-06-25T09:16:47.019529+00:00 app[web.1]: }
2023-06-25T09:16:47.019529+00:00 app[web.1]: 
2023-06-25T09:16:47.019530+00:00 app[web.1]: Node.js v18.16.1

package.json文件:

{
  "name": "herokuapp1",
  "version": "1.0.0",
  "description": "",
  "main": "node app.js",
  "scripts": {
    "start": "node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://git.heroku.com/herokuapp1.git"
  },
  "keywords": [
    "herokuapp1"
  ],
  "author": "John Doe",
  "license": "ISC"
}
英文:

Any assistance with the below would be highly appreciated. I've received a series of errors, most of which I was able to resolve, but one persists:

> Error: Cannot find module '/app/app.js'

I was able to fix similar errors before by adding details to the package.json file, such as the start line in the scripts block. I also tried moving the app.js file to the root directory, updating the reference in the HTML code, and committing and deploying again.

The build appears as successful in terminal. However, when I click on "Open App" on the Heroku website, the app fails and the logs reveal the below. Have any of you received this error? If so, how did you fix it?

Heroku log:

2023-06-25T09:16:43.627247+00:00 heroku[web.1]: State changed from starting to crashed
2023-06-25T09:16:43.632314+00:00 heroku[web.1]: State changed from crashed to starting
2023-06-25T09:16:45.545297+00:00 heroku[web.1]: Starting process with command `npm start`
2023-06-25T09:16:46.987365+00:00 app[web.1]: 
2023-06-25T09:16:46.987385+00:00 app[web.1]: > herokuapp1@1.0.0 start
2023-06-25T09:16:46.987385+00:00 app[web.1]: > node app.js
2023-06-25T09:16:46.987386+00:00 app[web.1]: 
2023-06-25T09:16:47.019524+00:00 app[web.1]: node:internal/modules/cjs/loader:1080
2023-06-25T09:16:47.019526+00:00 app[web.1]: throw err;
2023-06-25T09:16:47.019526+00:00 app[web.1]: ^
2023-06-25T09:16:47.019526+00:00 app[web.1]: 
2023-06-25T09:16:47.019526+00:00 app[web.1]: Error: Cannot find module '/app/app.js'
2023-06-25T09:16:47.019527+00:00 app[web.1]: at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
2023-06-25T09:16:47.019527+00:00 app[web.1]: at Module._load (node:internal/modules/cjs/loader:922:27)
2023-06-25T09:16:47.019528+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
2023-06-25T09:16:47.019528+00:00 app[web.1]: at node:internal/main/run_main_module:23:47 {
2023-06-25T09:16:47.019528+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2023-06-25T09:16:47.019529+00:00 app[web.1]: requireStack: []
2023-06-25T09:16:47.019529+00:00 app[web.1]: }
2023-06-25T09:16:47.019529+00:00 app[web.1]: 
2023-06-25T09:16:47.019530+00:00 app[web.1]: Node.js v18.16.1

package.json file:

{
  "name": "herokuapp1",
  "version": "1.0.0",
  "description": "",
  "main": "node app.js",
  "scripts": {
    "start": "node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://git.heroku.com/herokuapp1.git"
  },
  "keywords": [
    "herokuapp1"
  ],
  "author": "John Doe",
  "license": "ISC"
}

答案1

得分: 0

"main": "node app.js",

改为:

"main": "app.js",

更新:

对于访问此答案的其他人,用户成功使用以下方法:

添加一个像这样的 Procfile。

英文:

Try changing :

"main": "node app.js",

to :

"main": "app.js",

Update:

For people visiting this answer, above + following worked for the user:

Add a procfile like this.

答案2

得分: 0

Update: 错误已解决。根据subdevelopersuggestion,我在根目录中添加了一个 procfile。它有一行:

web: node app.js

错误不再出现。(现在出现了一个新的错误,但由于它与JavaScript代码中的 'window' 有关,我将其移到一个新的线程。)

英文:

Update: Error resolved. Per subdeveloper's (suggestion), I added a procfile to the root directory. It had one line:

web: node app.js

The error is no longer appears. (There is a new error popping up, but since it's related to the 'window' in the Javascript code, I'll move that to a new thread.)

huangapple
  • 本文由 发表于 2023年6月25日 18:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定