Search code examples
javascriptheroku

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


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]: > [email protected] 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"
}

Solution

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