【问题标题】:Success deploy on Heroku but I get Application Error using Express + Node.JS在 Heroku 上成功部署,但使用 Express + Node.JS 出现应用程序错误
【发布时间】:2022-01-07 03:28:30
【问题描述】:

这是我第一次尝试部署,我选择Heroku,请见谅,我将其作为一个有趣的项目进行部署。

我已按照此站点说明发送至deploy Heroku

由于我在终端上看到的内容,我已经成功部署了它

-----> Building on the Heroku-20 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 14.x...
       Downloading and installing node 14.18.2...
       Using default npm version: 6.14.15
       
-----> Restoring cache
       - node_modules
       
-----> Installing dependencies
       Installing node modules
       
       > ejs@2.7.4 postinstall /tmp/build_b0e24fab/node_modules/ejs
       > node ./postinstall.js
       
       
       > fsevents@1.2.13 install /tmp/build_b0e24fab/node_modules/fsevents
       > node install.js
       
       
       Skipping 'fsevents' build as platform linux is not supported
       
       > nodemon@1.18.5 postinstall /tmp/build_b0e24fab/node_modules/nodemon
       > node bin/postinstall || exit 0
       
       Love nodemon? You can now support the project via the open collective:
        > https://opencollective.com/nodemon/donate
       
       added 458 packages in 4.084s
       
-----> Build
       
-----> Caching build
       - node_modules
       
-----> Pruning devDependencies
       audited 458 packages in 1.789s
       
       32 packages are looking for funding
         run `npm fund` for details
       
       found 4 vulnerabilities (3 moderate, 1 high)
         run `npm audit fix` to fix them, or `npm audit` for details
       
-----> Build succeeded!
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 36.7M
-----> Launching...
       Released v5
       https://kanadb.herokuapp.com/ deployed to Heroku

但是当我尝试访问Heroku 提供给我的URL 时,它说应用程序错误。我试着去日志看看会发生什么。

这是日志告诉我的:

2022-01-06T16:21:23.381226+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2022-01-06T16:21:23.381226+00:00 app[web.1]:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2022-01-06T16:21:23.381227+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2022-01-06T16:21:23.381227+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
2022-01-06T16:21:23.381228+00:00 app[web.1]:     at internal/main/run_main_module.js:17:47 {
2022-01-06T16:21:23.381237+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
2022-01-06T16:21:23.381238+00:00 app[web.1]:   requireStack: []
2022-01-06T16:21:23.381238+00:00 app[web.1]: }
2022-01-06T16:21:23.541784+00:00 heroku[web.1]: Process exited with status 1
2022-01-06T16:21:23.625528+00:00 heroku[web.1]: State changed from starting to crashed
2022-01-06T16:32:41.768068+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kanadb.herokuapp.com request_id=645c04d9-37e3-49d1-9215-7100a50e6ed6 fwd="42.119.203.224" dyno= connect= service= status=503 bytes= protocol=https
2022-01-06T16:32:42.482576+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=kanadb.herokuapp.com request_id=4310b722-24f5-4b2c-98cf-9b7f2d1ab4bb fwd="42.119.203.224" dyno= connect= service= status=503 bytes= protocol=https

这是我的 ExpressJS 代码,它只做 1 个POST route,每当我点击我的Front-end 中的项目时,它会发送一个URL 到那个POST route 并修改那个URL 以提取我需要的数据并插入到我的Supabase

var PORT = process.env.PORT || 8000
const axios = require('axios')
const cheerio = require('cheerio')
const express = require('express')
const app = express()
const { createClient } = require('@supabase/supabase-js')
const supabase = createClient(
    'mySupabaseURL',
    'myAnonAPIKey'
  )

let cors = require('cors')
app.use(cors({
    origin: 'http://localhost:3000',
    credentials: true
}))

app.use(express.static("public"))
app.use(express.json())
app.use(express.urlencoded({extended: true}))

app.post('/', async function (req, res, next) {
    let animeData = []
    res.status(201).json({message: 'success'})
    let basedURL = req.body

    await axios(basedURL.animeURL)
        .then(async response =>{
            let html = response.data
            let $ = cheerio.load(html, {xmlMode: true})

            $('script', html).each(function(){
                let animeEpisode = $(this).text()
                animeData.push({
                  animeEpisode
                })
            })
            let result = Object.entries(animeData[15])
            let stringResult = result[0].toString()
            let arrayResult = stringResult.split(",")
            for(let i = 0; i<arrayResult.length; i++){
                let fboLink2 = arrayResult[i]
                let location = fboLink2.indexOf(`source_fbo: [{"file":"`)

                if(location != -1){
                    let newFBO = fboLink2.replace(`source_fbo: [{"file":"`, "")
                    let finalFBO = newFBO.replace(`"}]`, "")
                    const {data} = await supabase
                    .from('anime_detail')
                    .update({anime_video: finalFBO})
                    .match({anime_title: basedURL.animeTitle, anime_url: basedURL.animeURL, anime_episode: basedURL.anime_episode})
                }
            }
        }).catch(err => console.log(err))
  })


app.listen(PORT, ()=> {console.log(`Server is running`)})

当我在本地运行 Node.JS + ExpressFront-end 时,一切正常,所以我不知道是什么导致了问题。

这是我的结构,我也插入了Procfileweb: node index.js

更新 1:

使用web: node ./src/index.js 修复Procfile 后,需要index.js 的错误已修复,但这导致我遇到另一个我真的不知道如何修复的问题。

这是它的日志:

2022-01-06T19:22:51.002284+00:00 app[web.1]: internal/modules/cjs/loader.js:905
2022-01-06T19:22:51.002303+00:00 app[web.1]:   throw err;
2022-01-06T19:22:51.002304+00:00 app[web.1]:   ^
2022-01-06T19:22:51.002304+00:00 app[web.1]: 
2022-01-06T19:22:51.002304+00:00 app[web.1]: Error: Cannot find module '@supabase/supabase-js'
2022-01-06T19:22:51.002304+00:00 app[web.1]: Require stack:
2022-01-06T19:22:51.002305+00:00 app[web.1]: - /app/src/index.js
2022-01-06T19:22:51.002305+00:00 app[web.1]:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at Module.require (internal/modules/cjs/loader.js:974:19)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at require (internal/modules/cjs/helpers.js:93:18)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at Object.<anonymous> (/app/src/index.js:9:26)
2022-01-06T19:22:51.002307+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:1085:14)
2022-01-06T19:22:51.002307+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2022-01-06T19:22:51.002308+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2022-01-06T19:22:51.002308+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
2022-01-06T19:22:51.002308+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
2022-01-06T19:22:51.002309+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
2022-01-06T19:22:51.002309+00:00 app[web.1]:   requireStack: [ '/app/src/index.js' ]
2022-01-06T19:22:51.002309+00:00 app[web.1]: }
2022-01-06T19:22:51.275795+00:00 heroku[web.1]: State changed from starting to crashed
2022-01-06T19:22:48.000000+00:00 app[api]: Build succeeded

【问题讨论】:

    标签: node.js express heroku heroku-cli


    【解决方案1】:

    Heroku 找不到您的 index.js 文件:

    错误:找不到模块'/app/index.js'

    您的index.js 似乎位于名为src/ 的目录中。这应该反映在您的Procfile

    web: node src/index.js
    

    【讨论】:

    • 啊,我仔细看了看,它缺少 @supabase 依赖项,奇怪的是我的 package.json 中还有另一个 supabase 依赖项,但这无关紧要。我通过在我的依赖项中安装另一个 @supabase 解决了我的问题。
    猜你喜欢
    • 2014-09-16
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2017-02-04
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多