为啥当我的 heroku 应用程序崩溃时出现 mongodb 错误? (在本地工作就好了!)

Posted

技术标签:

【中文标题】为啥当我的 heroku 应用程序崩溃时出现 mongodb 错误? (在本地工作就好了!)【英文标题】:Why do I get a mongodb error when my heroku app crashes? (works locally just fine!)为什么当我的 heroku 应用程序崩溃时出现 mongodb 错误? (在本地工作就好了!) 【发布时间】:2021-05-22 02:37:57 【问题描述】:

这是我第一次将我的应用程序部署到 Heroku,当我在本地运行它时一切正常,但我无法让它实时启动。我在下面不断收到这些错误,我尝试删除并重新安装没有修复它的 node_modules。如果需要更多信息,请告诉我!

heroku 日志 --tail

PS C:\Users\Brandon\OneDrive\Desktop\IMS> heroku logs --tail
2021-02-19T04:15:59.895979+00:00 app[web.1]: '/app/node_modules/mongoose/index.js',
2021-02-19T04:15:59.895979+00:00 app[web.1]: '/app/app.js'
2021-02-19T04:15:59.895979+00:00 app[web.1]: ]
2021-02-19T04:15:59.895979+00:00 app[web.1]: 
2021-02-19T04:15:59.954837+00:00 heroku[web.1]: Process exited with status 1
2021-02-19T04:16:00.037960+00:00 heroku[web.1]: State changed from starting to crashed
2021-02-19T04:16:00.043603+00:00 heroku[web.1]: State changed from crashed to starting
2021-02-19T04:16:03.967774+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-02-19T04:16:06.007916+00:00 app[web.1]: internal/modules/cjs/loader.js:796
2021-02-19T04:16:06.007931+00:00 app[web.1]: throw err;
2021-02-19T04:16:06.007932+00:00 app[web.1]: ^
2021-02-19T04:16:06.007932+00:00 app[web.1]:
2021-02-19T04:16:06.007932+00:00 app[web.1]: Error: Cannot find module './mongodb_aws'
2021-02-19T04:16:06.007932+00:00 app[web.1]: Require stack:
2021-02-19T04:16:06.007933+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/auth/defaultAuthProviders.js
2021-02-19T04:16:06.007933+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/connection/connect.js
2021-02-19T04:16:06.007933+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/connection/pool.js
2021-02-19T04:16:06.007933+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/topologies/server.js
2021-02-19T04:16:06.007934+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/index.js
2021-02-19T04:16:06.007934+00:00 app[web.1]: - /app/node_modules/mongodb/index.js
2021-02-19T04:16:06.007934+00:00 app[web.1]: - /app/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js
2021-02-19T04:16:06.007934+00:00 app[web.1]: - /app/node_modules/mongoose/lib/drivers/node-mongodb-native/index.js
2021-02-19T04:16:06.007934+00:00 app[web.1]: - /app/node_modules/mongoose/lib/index.js
2021-02-19T04:16:06.007935+00:00 app[web.1]: - /app/node_modules/mongoose/index.js
2021-02-19T04:16:06.007935+00:00 app[web.1]: - /app/app.js
2021-02-19T04:16:06.007935+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
2021-02-19T04:16:06.007935+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:686:27)
2021-02-19T04:16:06.007935+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:848:19)
2021-02-19T04:16:06.007936+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2021-02-19T04:16:06.007954+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/mongodb/lib/core/auth/defaultAuthProviders.js:9:20)
2021-02-19T04:16:06.007955+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:955:30)
2021-02-19T04:16:06.007955+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
2021-02-19T04:16:06.007955+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:811:32)
2021-02-19T04:16:06.007956+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:723:14)
2021-02-19T04:16:06.007956+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:848:19) 
2021-02-19T04:16:06.007956+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-02-19T04:16:06.007957+00:00 app[web.1]: requireStack: [
2021-02-19T04:16:06.007957+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/auth/defaultAuthProviders.js',
2021-02-19T04:16:06.007957+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/connection/connect.js',
2021-02-19T04:16:06.007958+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/connection/pool.js',
2021-02-19T04:16:06.007958+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/topologies/server.js',
2021-02-19T04:16:06.007958+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/index.js',
2021-02-19T04:16:06.007959+00:00 app[web.1]: '/app/node_modules/mongodb/index.js',
2021-02-19T04:16:06.007959+00:00 app[web.1]: '/app/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js',
2021-02-19T04:16:06.007959+00:00 app[web.1]: '/app/node_modules/mongoose/lib/drivers/node-mongodb-native/index.js',
2021-02-19T04:16:06.007959+00:00 app[web.1]: '/app/node_modules/mongoose/lib/index.js',
2021-02-19T04:16:06.007960+00:00 app[web.1]: '/app/node_modules/mongoose/index.js',
2021-02-19T04:16:06.007960+00:00 app[web.1]: '/app/app.js'
2021-02-19T04:16:06.007961+00:00 app[web.1]: ]
2021-02-19T04:16:06.007961+00:00 app[web.1]: 
2021-02-19T04:16:06.049872+00:00 heroku[web.1]: Process exited with status 1
2021-02-19T04:16:06.107549+00:00 heroku[web.1]: State changed from starting to crashed
2021-02-19T04:17:15.212927+00:00 heroku[web.1]: State changed from crashed to starting
2021-02-19T04:17:20.276476+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-02-19T04:17:23.274167+00:00 app[web.1]: internal/modules/cjs/loader.js:796
2021-02-19T04:17:23.274177+00:00 app[web.1]: throw err;
2021-02-19T04:17:23.274178+00:00 app[web.1]: ^
2021-02-19T04:17:23.274178+00:00 app[web.1]:
2021-02-19T04:17:23.274179+00:00 app[web.1]: Error: Cannot find module './mongodb_aws'
2021-02-19T04:17:23.274182+00:00 app[web.1]: Require stack:
2021-02-19T04:17:23.274183+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/auth/defaultAuthProviders.js
2021-02-19T04:17:23.274183+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/connection/connect.js
2021-02-19T04:17:23.274183+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/connection/pool.js
2021-02-19T04:17:23.274184+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/topologies/server.js
2021-02-19T04:17:23.274184+00:00 app[web.1]: - /app/node_modules/mongodb/lib/core/index.js
2021-02-19T04:17:23.274185+00:00 app[web.1]: - /app/node_modules/mongodb/index.js
2021-02-19T04:17:23.274185+00:00 app[web.1]: - /app/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js
2021-02-19T04:17:23.274185+00:00 app[web.1]: - /app/node_modules/mongoose/lib/drivers/node-mongodb-native/index.js
2021-02-19T04:17:23.274186+00:00 app[web.1]: - /app/node_modules/mongoose/lib/index.js
2021-02-19T04:17:23.274186+00:00 app[web.1]: - /app/node_modules/mongoose/index.js
2021-02-19T04:17:23.274187+00:00 app[web.1]: - /app/app.js
2021-02-19T04:17:23.274187+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
2021-02-19T04:17:23.274187+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:686:27)
2021-02-19T04:17:23.274188+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:848:19)
2021-02-19T04:17:23.274188+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2021-02-19T04:17:23.274200+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/mongodb/lib/core/auth/defaultAuthProviders.js:9:20)
2021-02-19T04:17:23.274201+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:955:30)
2021-02-19T04:17:23.274201+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
2021-02-19T04:17:23.274202+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:811:32)
2021-02-19T04:17:23.274202+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:723:14)
2021-02-19T04:17:23.274202+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:848:19) 
2021-02-19T04:17:23.274203+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-02-19T04:17:23.274203+00:00 app[web.1]: requireStack: [
2021-02-19T04:17:23.274204+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/auth/defaultAuthProviders.js',
2021-02-19T04:17:23.274204+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/connection/connect.js',
2021-02-19T04:17:23.274204+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/connection/pool.js',
2021-02-19T04:17:23.274205+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/topologies/server.js',
2021-02-19T04:17:23.274205+00:00 app[web.1]: '/app/node_modules/mongodb/lib/core/index.js',
2021-02-19T04:17:23.274206+00:00 app[web.1]: '/app/node_modules/mongodb/index.js',
2021-02-19T04:17:23.274206+00:00 app[web.1]: '/app/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js',
2021-02-19T04:17:23.274206+00:00 app[web.1]: '/app/node_modules/mongoose/lib/drivers/node-mongodb-native/index.js',
2021-02-19T04:17:23.274207+00:00 app[web.1]: '/app/node_modules/mongoose/lib/index.js',
2021-02-19T04:17:23.274207+00:00 app[web.1]: '/app/node_modules/mongoose/index.js',
2021-02-19T04:17:23.274208+00:00 app[web.1]: '/app/app.js'
2021-02-19T04:17:23.274208+00:00 app[web.1]: ]
2021-02-19T04:17:23.274208+00:00 app[web.1]: 
2021-02-19T04:17:23.383905+00:00 heroku[web.1]: Process exited with status 1
2021-02-19T04:17:23.471665+00:00 heroku[web.1]: State changed from starting to crashed
2021-02-19T04:17:25.230199+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=ims-project.herokuapp.com request_id=58b97c7b-e354-4bbc-85b2-1801b527d414 fwd="75.131.204.202" dyno= connect= service= status=503 bytes= protocol=https
2021-02-19T04:17:25.359883+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=ims-project.herokuapp.com request_id=c824e667-9ef0-4fa4-9433-b755257050bf fwd="75.131.204.202" dyno= connect= service= status=503 bytes= protocol=https

package.json 文件:


  "name": "ims",
  "version": "1.0.0",
  "description": "Inventory Management Solution",
  "main": "app.js",
  "engines": 
    "node": "12.14.1"
  ,
  "scripts": 
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
    "start:dev": "nodemon -e js,ejs,html app.js"
  ,
  "author": "Brandon Kunkel",
  "license": "ISC",
  "dependencies": 
    "bcryptjs": "^2.4.3",
    "compression": "^1.7.4",
    "connect-flash": "^0.1.1",
    "connect-mongodb-session": "^2.4.1",
    "csurf": "^1.11.0",
    "debug": "^4.3.1",
    "ejs": "^3.1.5",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "express-validation": "^3.0.5",
    "express-validator": "^6.6.1",
    "helmet": "^4.4.1",
    "lodash": "^4.17.20",
    "mongodb": "^3.6.0",
    "mongoose": "^5.10.2",
    "nodemailer": "^6.4.11",
    "nodemailer-sendgrid-transport": "^0.2.0",
    "nodemon": "^2.0.4",
    "pdfkit": "^0.11.0"
  

Heroku 构建日志:

-----> Building on the Heroku-20 stack
-----> 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):  12.14.1
       engines.npm (package.json):   unspecified (use default)
       
       Resolving node version 12.14.1...
       Downloading and installing node 12.14.1...
       Using default npm version: 6.13.4
       
-----> Restoring cache
       - node_modules is checked into source control and cannot be cached
       
-----> Installing dependencies
       Prebuild detected (node_modules already exists)
       Rebuilding any native modules
       
       > core-js@2.6.12 postinstall /tmp/build_6a47509d/node_modules/core-js
       > node -e "tryrequire('./postinstall')catch(e)"
       
       
       > nodemon@2.0.7 postinstall /tmp/build_6a47509d/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

然后我删除了一堆文件,因为我超过了字符数限制,除了我在下面直接列出了涉及 mongo 的任何内容:

connect-mongodb-session@2.4.1 /tmp/build_6a47509d/node_modules/connect-mongodb-session
mongodb@3.6.4 /tmp/build_6a47509d/node_modules/mongodb
mongoose@5.11.17 /tmp/build_6a47509d/node_modules/mongoose
mongoose-legacy-pluralize@1.0.2 /tmp/build_6a47509d/node_modules/mongoose-legacy-pluralize
mpath@0.8.3 /tmp/build_6a47509d/node_modules/mongoose/node_modules/mpath
safe-buffer@5.2.1 /tmp/build_6a47509d/node_modules/mongoose/node_modules/safe-buffer

Installing any new modules (package.json)
       added 136 packages from 188 contributors and audited 485 packages in 6.594s
       
       26 packages are looking for funding
         run `npm fund` for details
       
       found 10 vulnerabilities (7 low, 1 moderate, 2 high)
         run `npm audit fix` to fix them, or `npm audit` for details
       
-----> Build
       
-----> Caching build
       - node_modules
       
-----> Pruning devDependencies
       audited 485 packages in 3.211s
       
       26 packages are looking for funding
         run `npm fund` for details
       
       found 10 vulnerabilities (7 low, 1 moderate, 2 high)
         run `npm audit fix` to fix them, or `npm audit` for details
       
-----> Build succeeded!
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 34.6M
-----> Launching...
       Released v16
       https://ims-project.herokuapp.com/ deployed to Heroku

【问题讨论】:

部署时是否运行了 npm install ?因为它说找不到模块'./mongodb_aws' 你提交了带有 mongodb_aws 依赖的 package.json 吗? 删除 node_modules 后,我确实运行了 npm install,然后保存并推送到 heroku master。 mongodb 和 mongoose 都在 package.json 依赖文件中,并且都在当前 node_modules 文件夹中可见。下面我将列出 package.json 文件和我推送到 heroku 时的构建日志。 【参考方案1】:

对于任何有同样问题的人 - 我最终通过将源代码克隆到我的本地计算机来解决了这个问题。我一直在对原始本地文件夹进行故障排除。在我使用以下方法从 heroku 克隆它之后:

$ heroku git:clone -a ims-project
$ cd ims-project

我删除了 node_modules 文件夹和 package-lock.json 文件,然后再次运行 npm install。现在它正在工作!

【讨论】:

以上是关于为啥当我的 heroku 应用程序崩溃时出现 mongodb 错误? (在本地工作就好了!)的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在运行时出现整数精度错误:heroku run python manage.py migrate

在 Heroku 上部署 MEAN 应用程序时出现应用程序错误

为啥我在 heroku 上的应用程序在 30 秒后崩溃?

Firebase Android:当我的数据库增长时出现问题

首次点击 UITextField 时出现奇怪的崩溃

在heroku上部署应用程序时出现过时的纱线锁文件错误