简体   繁体   English

Heroku 错误。 找不到模块“猫鼬”

[英]Heroku Error. Cannot find module 'Mongoose'

For some odd reason (i have been deployed on Heroku for around 1.5 years) my instance decided to throw a weird error regarding not finding 'mongoose' after attempting to deploy again.出于某种奇怪的原因(我已经在 Heroku 上部署了大约 1.5 年),我的实例决定在尝试再次部署后抛出一个关于未找到“猫鼬”的奇怪错误。 Everything works on my local server and my .gitignore file ignores Node Modules.一切都在我的本地服务器上运行,我的 .gitignore 文件忽略了节点模块。 This is a React.js app with Expressjs and Mongoose.这是一个带有 Expressjs 和 Mongoose 的 React.js 应用程序。

Here is my Package.json:这是我的 Package.json:

    {
  "name": "drg-coming-soon",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "server": "node server/index.js",
    "start": "webpack && node server/index.js",
    "react": "webpack -d --watch"
  },
  "heroku-run-build-script": true,
  "author": "Randy Thomas",
  "license": "ISC",
  "devDependencies": {
    "axios": "^0.18.0",
    "babel-core": "^6.23.1",
    "babel-loader": "^6.3.2",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "body-parser": "^1.18.3",
    "express": "^4.15.0",
    "jquery": "^3.1.1",
    "mongoose": "^6.2.7",
    "react-awesome-modal": "^2.0.5",
    "request": "^2.81.0",
    "webpack": "^4.28.3"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "css-loader": "^2.0.1",
    "dotenv": "^6.2.0",
    "file-loader": "^2.0.0",
    "react": "^15.4.2",
    "react-autosuggest": "^9.4.3",
    "react-dom": "^15.4.2",
    "react-image": "^1.5.1",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "^2.1.1",
    "style-loader": "^0.23.1",
    "twilio": "^3.33.2",
    "url-loader": "^1.1.2",
    "mongoose": "^6.2.7",
    "webpack": "^4.28.3"
  }
}

Here is my webpack.config.js这是我的 webpack.config.js

const path = require('path');

const SRC_DIR = path.join(__dirname, '/client/src');
const DIST_DIR = path.join(__dirname, '/client/dist');

module.exports = {
  entry: `${SRC_DIR}/index.js`,
  output: {
    filename: 'bundle.js',
    path: DIST_DIR,
  },
  module: {
    //changed from loaders to rules
    loaders: [
      {
        test: /\.jsx?/,
        include: SRC_DIR,
        loader: 'babel-loader',
        query: {
          presets: [
            '@babel/preset-env',
            '@babel/preset-react'
          ],
        },
      },
    ],
  },
};

Here is my Database folder:这是我的数据库文件夹:

require('dotenv').config();
const mongoose = require('mongoose');
// dev

// process.env.mongourl

mongoose.connect(process.env.mongourl)
  .catch(err => console.log('Mongo connection error', err));


const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
  console.log('MongoDB has connected');
});

// schemas
const comingSoonSchema = ({
  address: String,
  desc: String,
  sqft: Number,
  bed: String,
  bath: String,
  photoLink: String,
  agent: String,
  price: String,
  year: Number,
  eta: String,
  premarket: String,
  status: String,
  timeStamp: { type: Date, default: Date.now },
})


// models
const ComingSoon = mongoose.model('ComingSoon', comingSoonSchema);

function save(e) {
  console.log(e, "SAVE FUNC");
  const obj = new ComingSoon({
    address: e.address,
    desc: e.desc,
    sqft: e.sqft,
    bed: e.bed,
    bath: e.bath,
    photoLink: e.photoLink,
    agent: e.agent,
    price: e.price,
    year: e.year,
    eta: e.eta,
    status: e.status,
    premarket: e.premarket
  })
  obj.save();
  console.log("Data saved to MongoDB Database");
}

const funcs = {
  save, ComingSoon,
};
module.exports = funcs;

And lastly here is the persisting error:最后是持续错误:

2022-03-23T01:33:06.685994+00:00 heroku[web.1]: Starting process with command `npm start`
2022-03-23T01:33:07.919434+00:00 app[web.1]:
2022-03-23T01:33:07.919447+00:00 app[web.1]: > drg-coming-soon@1.0.0 start
2022-03-23T01:33:07.919448+00:00 app[web.1]: > webpack && node server/index.js
2022-03-23T01:33:07.919448+00:00 app[web.1]:
2022-03-23T01:33:07.965317+00:00 app[web.1]: One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
2022-03-23T01:33:07.965319+00:00 app[web.1]: - webpack-cli (https://github.com/webpack/webpack-cli)
2022-03-23T01:33:07.965319+00:00 app[web.1]: The original webpack full-featured CLI.
2022-03-23T01:33:07.965784+00:00 app[web.1]: We will use "npm" to install the CLI via "npm install -D".
2022-03-23T01:33:08.131129+00:00 app[web.1]: Do you want to install 'webpack-cli' (yes/no): node:internal/modules/cjs/loader:936
2022-03-23T01:33:08.131131+00:00 app[web.1]: throw err;
2022-03-23T01:33:08.131131+00:00 app[web.1]: ^
2022-03-23T01:33:08.131132+00:00 app[web.1]:
2022-03-23T01:33:08.131132+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2022-03-23T01:33:08.131132+00:00 app[web.1]: Require stack:
2022-03-23T01:33:08.131133+00:00 app[web.1]: - /app/database/index.js
2022-03-23T01:33:08.131136+00:00 app[web.1]: - /app/server/index.js
2022-03-23T01:33:08.131136+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-03-23T01:33:08.131137+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-03-23T01:33:08.131137+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-03-23T01:33:08.131138+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)
2022-03-23T01:33:08.131138+00:00 app[web.1]: at Object.<anonymous> (/app/database/index.js:2:18)
2022-03-23T01:33:08.131139+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-03-23T01:33:08.131139+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
2022-03-23T01:33:08.131140+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32)
2022-03-23T01:33:08.131140+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-03-23T01:33:08.131140+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19) {
2022-03-23T01:33:08.131141+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-03-23T01:33:08.131141+00:00 app[web.1]: requireStack: [ '/app/database/index.js', '/app/server/index.js' ]
2022-03-23T01:33:08.131142+00:00 app[web.1]: }
2022-03-23T01:33:08.265674+00:00 heroku[web.1]: Process exited with status 1
2022-03-23T01:33:08.447674+00:00 heroku[web.1]: State changed from starting to crashed
2022-03-23T01:32:58.202124+00:00 app[api]: Release v158 created by user bookingrlthomas@gmail.com
2022-03-23T01:32:58.202124+00:00 app[api]: Deploy ad551b5e by user bookingrlthomas@gmail.com
2022-03-23T01:33:00.927332+00:00 app[api]: Deploy ad551b5e by user bookingrlthomas@gmail.com
2022-03-23T01:33:00.927332+00:00 app[api]: Release v159 created by user bookingrlthomas@gmail.com
2022-03-23T01:33:51.331557+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=drgcomingsoonlistings.herokuapp.com request_id=fd27788b-e166-49fd-8830-b0da493e7e62 fwd="47.45.81.207" dyno= connect= service= status=503 bytes= protocol=https
2022-03-23T01:33:52.166634+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=drgcomingsoonlistings.herokuapp.com request_id=4967681e-a5a9-41b9-9129-65258f9b9983 fwd="47.45.81.207" dyno= connect= service= status=503 bytes= protocol=https

The error is most likely coming from the fact that you have mongoose in your "devDependencies".该错误很可能来自您的“devDependencies”中有猫鼬这一事实。 Try moving it to your "dependencies", then running npm install.尝试将其移至您的“依赖项”,然后运行 ​​npm install。 You may run into the same issue with express and a few others under "devDependencies".您可能会在“devDependencies”下遇到 express 和其他一些问题。 Hope this helps.希望这可以帮助。

It does not make any sense, but if mongoose is both in dev dependencies and in dependencies, heroku goes nuts.这没有任何意义,但如果 mongoose 既在开发依赖项中又在依赖项中,heroku 就会发疯。 It can be ONLY in dependencies.它只能在依赖项中。 After deleting it from dev dependencies, do npm install.从开发依赖项中删除它后,执行 npm install。 It will work without any issues.它将毫无问题地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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