Heroku部署后找不到猫鼬模块?

时间:2020-10-17 02:17:33

标签: node.js mongodb express heroku mongoose

我已经在Heroku上部署了一个简单的Nodejs应用程序,并且在应用程序日志中不断出现错误,提示找不到模块“猫鼬”。我什至提供了一条相对路径,而不仅仅是说require(“猫鼬”)。部署成功并安装所有依赖项。可以在https://github.com/collinkleest/trim-io上找到源代码。

这是来自heroku的错误日志。

2020-10-17T02:01:42.000000+00:00 app[api]: Build started by user collinkleest@gmail.com
2020-10-17T02:01:59.000000+00:00 app[api]: Build succeeded
2020-10-17T02:01:59.493929+00:00 app[api]: Deploy af985be5 by user collinkleest@gmail.com
2020-10-17T02:01:59.493929+00:00 app[api]: Release v14 created by user collinkleest@gmail.com
2020-10-17T02:01:59.652847+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-17T02:02:01.917601+00:00 heroku[web.1]: Starting process with command `npm start`
2020-10-17T02:02:04.175589+00:00 app[web.1]:
2020-10-17T02:02:04.175612+00:00 app[web.1]: > trim-io@0.0.1 start /app
2020-10-17T02:02:04.175612+00:00 app[web.1]: > node server.js
2020-10-17T02:02:04.175613+00:00 app[web.1]:
2020-10-17T02:02:04.369536+00:00 app[web.1]: internal/modules/cjs/loader.js:834
2020-10-17T02:02:04.369538+00:00 app[web.1]: throw err;
2020-10-17T02:02:04.369539+00:00 app[web.1]: ^
2020-10-17T02:02:04.369539+00:00 app[web.1]:
2020-10-17T02:02:04.369539+00:00 app[web.1]: Error: Cannot find module '../node_modules/mongoose'
2020-10-17T02:02:04.369540+00:00 app[web.1]: Require stack:
2020-10-17T02:02:04.369540+00:00 app[web.1]: - /app/models/urlEntity.js
2020-10-17T02:02:04.369541+00:00 app[web.1]: - /app/server.js
2020-10-17T02:02:04.369541+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:831:15)
2020-10-17T02:02:04.369542+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:687:27)
2020-10-17T02:02:04.369542+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:903:19)
2020-10-17T02:02:04.369543+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2020-10-17T02:02:04.369543+00:00 app[web.1]: at Object.<anonymous> (/app/models/urlEntity.js:1:18)
2020-10-17T02:02:04.369543+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1015:30)
2020-10-17T02:02:04.369544+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
2020-10-17T02:02:04.369544+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:879:32)
2020-10-17T02:02:04.369545+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:724:14)
2020-10-17T02:02:04.369545+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:903:19) {
2020-10-17T02:02:04.369545+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2020-10-17T02:02:04.369546+00:00 app[web.1]: requireStack: [ '/app/models/urlEntity.js', '/app/server.js' ]
2020-10-17T02:02:04.369546+00:00 app[web.1]: }
2020-10-17T02:02:04.389361+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-10-17T02:02:04.389827+00:00 app[web.1]: npm ERR! errno 1
2020-10-17T02:02:04.398788+00:00 app[web.1]: npm ERR! trim-io@0.0.1 start: `node server.js`
2020-10-17T02:02:04.399025+00:00 app[web.1]: npm ERR! Exit status 1
2020-10-17T02:02:04.399280+00:00 app[web.1]: npm ERR!
2020-10-17T02:02:04.399493+00:00 app[web.1]: npm ERR! Failed at the trim-io@0.0.1 start script.
2020-10-17T02:02:04.399722+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-10-17T02:02:04.420468+00:00 app[web.1]:
2020-10-17T02:02:04.420707+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-10-17T02:02:04.420857+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-10-17T02_02_04_400Z-debug.log
2020-10-17T02:02:04.487055+00:00 heroku[web.1]: Process exited with status 1
2020-10-17T02:02:04.554273+00:00 heroku[web.1]: State changed from starting to crashed

这也是我的server.js供参考:

const express = require("express");
const UrlEntity = require("./models/urlEntity"); 
const mongoose = require("./node_modules/mongoose");
const urlEntity = require("./models/urlEntity");
const app = express();
require('dotenv').config();

const mongoUri = "mongodb+srv://" + process.env.MONGO_USR + ":" + process.env.MONGO_PASS + "@" + process.env.MONGO_URL + "/" + process.env.MONGO_DB + "?retryWrites=true&w=majority";

mongoose.connect(
    mongoUri,
    { useNewUrlParser: true, useUnifiedTopology: true}
);

app.use(express.json());
app.use(express.static('public'));

async function checkIdExists(id){
    let exists = await UrlEntity.exists({uniqueId: id});
    return exists;
}

function generateId(){
    let randomId = Math.random().toString(20).substr(2, 8);
    while(!(checkIdExists(randomId))){
        randomId = Math.random().toString(20).substr(2, 8);
    }
    return randomId;
}

// redirect functionality 
app.get("/:id", async (req, res) => {
    if (req.params.id !== null || req.params.id !== undefined){
        let doc = await UrlEntity.findOne({uniqueId: req.params.id});
        res.redirect(doc.targetUrl);
    } else {
        res.redirect('/');
    }
});

// create new url
app.post("/create-url", async (req, res) => {
    console.log("in post");
    let id = generateId();
    await UrlEntity.create({uniqueId: id,
    targetUrl: req.body.url,
    dateCreated: req.body.date});
    res.send(`${req.protocol}://${req.get('host')}/${id}`);
});

app.listen(process.env.PORT || 5000,  () => {console.log("Trim.io API Started")});

2 个答案:

答案 0 :(得分:1)

您将mongoose放入了devDependencies中,它将在运行时删除。 (Link from your code)您需要将其移至dependencies,以便在运行时可用。

答案 1 :(得分:0)

  • 尝试更改const mongoose = require("./node_modules/mongoose") 回到const mongoose = require("mongoose");

  • 不要忘记在heroku设置上设置环境变量 MONGO_USR MONGO_PASS 等。如果未设置环境变量,则服务器将无法启动。

  • 最后我建议您使用像这样的连接字符串mongodb+srv://user:password@cluster2.bftwu.mongodb.net/databaseName 没有连接