简体   繁体   中英

Heroku error after deployment - cannot find module 'mongoose'

I deployed a MERN app to Heroku.

I ran "npm run build" and added all of the frontend files into backend/public/ folder.

It starts the server after pushing it, because it logs me a "server is running" message in the console, but when I go on the website ( https://stonkappmern.herokuapp.com/ ) it gives me an application error ( works fine on localhost ).

For some reason it fails to work with this error in the terminal ( after running "Heroku logs --tail"):

2022-05-03T10:56:47.840874+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-03T10:56:47.840874+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)    
2022-05-03T10:56:47.840875+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:4:18)
2022-05-03T10:56:47.840875+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-05-03T10:56:47.840875+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
2022-05-03T10:56:47.840876+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32) 
2022-05-03T10:56:47.840876+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-03T10:56:47.840876+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
2022-05-03T10:56:47.840876+00:00 app[web.1]: at node:internal/main/run_main_module:17:47 {
2022-05-03T10:56:47.840877+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-05-03T10:56:47.840877+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2022-05-03T10:56:47.840878+00:00 app[web.1]: }     
2022-05-03T10:56:47.967637+00:00 heroku[web.1]: Process exited with status 1
2022-05-03T10:56:48.103967+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-03T10:57:30.237127+00:00 heroku[web.1]: State changed from crashed to starting
2022-05-03T10:57:32.901015+00:00 heroku[web.1]: Starting process with command `node index.js`
2022-05-03T10:57:34.655735+00:00 app[web.1]: node:internal/modules/cjs/loader:936
2022-05-03T10:57:34.655748+00:00 app[web.1]: throw err;
2022-05-03T10:57:34.655748+00:00 app[web.1]: ^     
2022-05-03T10:57:34.655749+00:00 app[web.1]:       
2022-05-03T10:57:34.655749+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2022-05-03T10:57:34.655749+00:00 app[web.1]: Require stack:
2022-05-03T10:57:34.655749+00:00 app[web.1]: - /app/index.js
2022-05-03T10:57:34.655750+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-03T10:57:34.655750+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-03T10:57:34.655751+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-03T10:57:34.655751+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)    
2022-05-03T10:57:34.655751+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:4:18)
2022-05-03T10:57:34.655751+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-05-03T10:57:34.655752+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
2022-05-03T10:57:34.655752+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32) 
2022-05-03T10:57:34.655752+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-03T10:57:34.655752+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
2022-05-03T10:57:34.655753+00:00 app[web.1]: at node:internal/main/run_main_module:17:47 {
2022-05-03T10:57:34.655753+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-05-03T10:57:34.655753+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2022-05-03T10:57:34.655753+00:00 app[web.1]: }     
2022-05-03T10:57:34.787391+00:00 heroku[web.1]: Process exited with status 1
2022-05-03T10:57:34.857608+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-03T11:01:02.000000+00:00 app[api]: Build failed -- check your build output: https://dashboard.heroku.com/apps/278242ea-f91b-4a30-b8f6-387d7b431a64/activity/builds/0d0f7825-53b1-4459-9f5a-39a5bfaaef51
2022-05-03T11:01:32.000000+00:00 app[api]: Build failed -- check your build output: https://dashboard.heroku.com/apps/278242ea-f91b-4a30-b8f6-387d7b431a64/activity/builds/0d0f7825-53b1-4459-9f5a-39a5bfaaef51
2022-05-03T11:01:38.000000+00:00 app[api]: Build started by user mattszumilo@gmail.com
2022-05-03T11:02:19.020534+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stonkappmern.herokuapp.com request_id=2f4b9b97-1b25-4194-9e80-193e0ccda091 fwd="172.58.122.180" dyno= connect= service= status=503 bytes= protocol=https
2022-05-03T11:02:19.355011+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stonkappmern.herokuapp.com request_id=688161a7-0d7b-43a1-85ee-265567deeef5 fwd="172.58.122.180" dyno= connect= service= status=503 bytes= protocol=https
2022-05-03T11:09:06.858371+00:00 heroku[web.1]: State changed from crashed to starting
2022-05-03T11:09:09.452545+00:00 heroku[web.1]: Starting process with command `node index.js`
2022-05-03T11:09:11.175268+00:00 app[web.1]: node:internal/modules/cjs/loader:936
2022-05-03T11:09:11.175282+00:00 app[web.1]: throw err;
2022-05-03T11:09:11.175282+00:00 app[web.1]: ^     
2022-05-03T11:09:11.175283+00:00 app[web.1]:       
2022-05-03T11:09:11.175283+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2022-05-03T11:09:11.175283+00:00 app[web.1]: Require stack:
2022-05-03T11:09:11.175283+00:00 app[web.1]: - /app/index.js
2022-05-03T11:09:11.175284+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-03T11:09:11.175285+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-03T11:09:11.175285+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-03T11:09:11.175286+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)    
2022-05-03T11:09:11.175286+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:4:18)
2022-05-03T11:09:11.175286+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-05-03T11:09:11.175286+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
2022-05-03T11:09:11.175286+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32) 
2022-05-03T11:09:11.175287+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-03T11:09:11.175287+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
2022-05-03T11:09:11.175287+00:00 app[web.1]: at node:internal/main/run_main_module:17:47 {
2022-05-03T11:09:11.175288+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-05-03T11:09:11.175288+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2022-05-03T11:09:11.175288+00:00 app[web.1]: }     
2022-05-03T11:09:11.301164+00:00 heroku[web.1]: Process exited with status 1
2022-05-03T11:09:11.442358+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-03T11:09:11.695307+00:00 heroku[web.1]: State changed from crashed to starting
2022-05-03T11:09:14.957519+00:00 heroku[web.1]: Starting process with command `node index.js`
2022-05-03T11:09:17.250325+00:00 app[web.1]: node:internal/modules/cjs/loader:936
2022-05-03T11:09:17.250429+00:00 app[web.1]: throw err;
2022-05-03T11:09:17.250430+00:00 app[web.1]: ^     
2022-05-03T11:09:17.250430+00:00 app[web.1]:       
2022-05-03T11:09:17.250431+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2022-05-03T11:09:17.250431+00:00 app[web.1]: Require stack:
2022-05-03T11:09:17.250431+00:00 app[web.1]: - /app/index.js
2022-05-03T11:09:17.250432+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-03T11:09:17.250432+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-03T11:09:17.250432+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-03T11:09:17.250433+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)    
2022-05-03T11:09:17.250433+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:4:18)
2022-05-03T11:09:17.250433+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-05-03T11:09:17.250434+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
2022-05-03T11:09:17.250434+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32) 
2022-05-03T11:09:17.250434+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-03T11:09:17.250435+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
2022-05-03T11:09:17.250435+00:00 app[web.1]: at node:internal/main/run_main_module:17:47 {
2022-05-03T11:09:17.250435+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-05-03T11:09:17.250436+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2022-05-03T11:09:17.250436+00:00 app[web.1]: }     
2022-05-03T11:09:17.445123+00:00 heroku[web.1]: Process exited with status 1
2022-05-03T11:09:17.572969+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-03T11:09:42.000000+00:00 app[api]: Build started by user mattszumilo@gmail.com
2022-05-03T11:10:21.037950+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=stonkappmern.herokuapp.com request_id=a310cd3a-96a2-46f7-9f57-3fa623ed2c06 fwd="172.58.122.180" dyno= connect= service= status=503 bytes= protocol=https
2022-05-03T11:10:21.345869+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=stonkappmern.herokuapp.com request_id=8fb05016-f42f-4bbb-a72b-864d45d80334 fwd="17est_id=bdc589b0-85e4-49af-aa36-d40285f58e41 fwd="172.58.122.180" dyno= connect= service= status=503 bytes= protocol=https

and here's my package.json file

{
  "name": "okkurwastocksscraper",
  "version": "1.0.0",
  "engines": {
    "node": "16.14.2",
    "npm": "6.14.16"
  },
  "main": "index.js",
  "scripts": {
    "start": "npm start",
    "build": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "body-parser": "^1.20.0",
    "cheerio": "^1.0.0-rc.10",
    "cors": "^2.8.5",
    "dotenv": "^16.0.0",
    "express": "^4.18.1",
    "express-ws": "^5.0.2",
    "fast-csv": "^4.3.6",
    "http-proxy-middleware": "^2.0.6",
    "longjohn": "^0.2.12",
    "mongo": "^0.1.0",
    "mongoose": "^6.3.2",
    "multer": "^1.4.4",
    "node-cron": "^3.0.0",
    "node-fetch": "^3.2.3",
    "nodemon": "^2.0.15",
    "request": "^2.88.2",
    "request-promise": "^4.2.6",
    "require": "^2.4.20"
  },
  "devDependencies": {
    "concurrently": "^7.1.0"
  }
}

I'm pretty sure I have mongoose installed - it's also in the package.json file.

In the backend file (index.js) I have:

const mongoose = require("mongoose");

async function connectToMongoDB() {
    await mongoose.connect(URI);
    // console.log("connected to mongodb");
};

Here's my folder structure:

enter image description here

Any ideas on what the issue could be? Should I upload more files?

I think your scripts is not correct.
https://docs.npmjs.com/cli/v8/using-npm/scripts#npm-start
for node.js part:
change your scripts in package.json like this:

  "scripts": {
    "start": "node index.js -p $PORT",
  },

heroku looks for start command to run your app and will assign a port to $PORT .
Based on your porject structure, your app does not require build at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM