Search code examples
node.jsexpressherokuheroku-cli

Success deploy on Heroku but I get Application Error using Express + Node.JS


This is my first time trying to deploy, and I choose Heroku, please pardon me, and I'm deploying it as a fun project for practice purposes.

I've followed this site instruction to deploy Heroku

And I've successfully deployed it due to what I read on the terminal

-----> 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

But when I tried to access that URL that Heroku provide me, it's said Application Error. And I tried to go to the logs to see what happen.

And here's what the logs tell me:

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

Here's my ExpressJS code, it's only do 1 POST route, whenever I click on the items in my Front-end, it'll send an URL down to that POST route and modify that URL to extract the data I need and insert into my 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`)})

Everything is working fine for me when I ran locally both Node.JS + Express and my Front-end, so I don't know what caused the problem.

This is my structure, and I've inserted the Procfile with web: node index.js, too.

Folder Structure

Update 1:

After fixing the Procfile with web: node ./src/index.js, the error that requires the index.js is fixed, but it lead me to another problem that I really don't know how to fix with.

Here's the log of it:

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

Solution

  • Heroku can't find your index.js file:

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

    It looks like your index.js is inside a directory called src/. That should be reflected in your Procfile:

    web: node src/index.js