2

My web works perfectly on my local machine and all that, but in Heroku I deployed my app and nothing is working, and when: $ heroku logs --tail

State changed from crashed to starting
Starting process with command `node app.js`
internal/modules/cjs/loader.js:883
throw err;
^

Error: Cannot find module 'express'
Require stack:
- /app/app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/app/app.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/app.js' ]
}

directory structure:

-app
 -css
 -files
 -js
 -logo
 -views
 -index.html
-app.js
-composer.json
-package-lock.json
-package.json
-Procfile

Procfile:

web: node app.js

package.json:

{
  "name": "app",
  "version": "1.0.0",
  "engines": {
    "node": "14.15.3"
  },
  "private": "true",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "keywords": [],
  "author": "me",
  "repository": "appRep",
  "license": "ISC",
  "devDependencies": {
    "chokidar": "^3.5.2",
    "express": "^4.17.2",
    "fs": "0.0.1-security",
    "nodemailer": "^6.7.2",
    "nodemon": "^2.0.15",
    "path": "^0.12.7"
  }
}

.env: (even before the .env file everything was the same)

PORT = 8081

Do you guys have any idea? Thank you in advance!!

2
  • Did you try putting app.js in the app folder?
    – yuuuu
    Jan 5, 2022 at 18:11
  • yes I did, that did not work at all, and plus that way my localserver can't find my files Jan 5, 2022 at 18:14

2 Answers 2

3

Your express package is listed in devDependencies in package.json, try moving it to dependencies like so

"dependencies": {
  "express": "^4.17.2"
}

devDependencies should list only packages which are not essential for app deployed in production (Heroku). It might be necessary to move more packages to dependencies not only express.

2
  • This worked!!!!!!!!!!!!!!!!!!!!!!!!!!! thank you good sir, can you explain why express should be in dependencies and not devDependencies? I am really curious... Jan 5, 2022 at 18:18
  • 1
    @Liemannenloop devDependencies are packages you need to use only during development, for example nodemon, so Heroku doesn't need to install it in production build; read more here stackoverflow.com/questions/18875674/… Jan 5, 2022 at 18:22
1

Make sure the express installed on Heroku. You should add express to the dependencies object in package.json file

2
  • Can you explain why this works? I hope I could upvote your comment but I have little reputation lol, but this is 100% correct answer as the previous gentlemen stated too. Jan 5, 2022 at 18:21
  • 1
    No problem :), best wishes to you. Jan 5, 2022 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.