部署后出现 Heroku 错误 - 找不到模块 'mongoose'

Heroku error after deployment - cannot find module 'mongoose'

我向 Heroku 部署了一个 MERN 应用程序。

我 运行 "npm 运行 build" 并将所有前端文件添加到 backend/public/ 文件夹中。

它在推送后启动服务器,因为它在控制台中记录了一条“服务器正在 运行ning”消息,但是当我访问网站时 (https://stonkappmern.herokuapp.com/) 它给了我应用程序错误(在本地主机上工作正常)。

出于某种原因,它无法在终端中处理此错误(在 运行ning“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

这是我的 package.json 文件

{
  "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"
  }
}

我很确定我已经安装了 mongoose - 它也在 package.json 文件中。

在后台文件中(index.js)我有:

const mongoose = require("mongoose");

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

这是我的文件夹结构:

enter image description here

关于问题可能是什么的任何想法?我应该上传更多文件吗?

我认为你的scripts不正确。
https://docs.npmjs.com/cli/v8/using-npm/scripts#npm-start
节点js部分:
像这样更改 package.json 中的 scripts

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

heroku 寻找 start 命令到 run 你的应用程序,并将分配一个端口给 $PORT.
根据您的项目结构,您的应用根本不需要 build