我从heroku收到上述错误。我尝试了几种解决方案,包括:在package.json中为节点版本和npm更新引擎,以及使用heroku restart命令。我还确保我的数据库配置上的端口没有按照某些答案建议进行硬编码。这是我的配置:
var url = process.env.DATABASEURL || "mongodb://localhost/roam-
hub";
mongoose.connect(url);
错误也指向我的dotenv文件,我认为该文件已正确连接和配置?在我的本地环境下,一切正常。有人对如何解决此问题有新的建议吗?
throw err;
2018-09-12T12:22:20.970580+00:00 app[web.1]: ^
2018-09-12T12:22:20.970582+00:00 app[web.1]:
2018-09-12T12:22:20.970583+00:00 app[web.1]: Error: Cannot find
module 'dotenv'
2018-09-12T12:22:20.970585+00:00 app[web.1]: at
Function.Module._resolveFilename
(internal/modules/cjs/loader.js:594:15)
这是我的package.json文件:
{
"name": "roam-app",
"version": "1.0.0",
"engines": {
"node": "10.5.0",
"npm": "6.2.0"
},
"description": "central app for all things Roam",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"author": "*",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"cloudinary": "^1.11.0",
"connect-flash": "^0.1.1",
"connect-timeout": "^1.9.0",
"cookie-parser": "^1.4.3",
"ejs": "^2.6.1",
"express": "^4.16.3",
"express-session": "^1.15.6",
"method-override": "^2.3.10",
"moment": "^2.22.2",
"mongoose": "^5.1.5",
"multer": "^1.3.1",
"nodemailer": "^4.6.7",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^5.0.0",
"request": "^2.87.0",
"session": "^0.1.0"
},
"devDependencies": {
"dotenv": "^6.0.0"
}
}
更改一些基本的HTML文件并将其推送到heroku之后,我开始收到此错误。预先感谢您的帮助。
答案 0 :(得分:1)
stacktrace告诉您出了什么问题,最重要的是:
Error: Cannot find module 'dotenv'
我怀疑您正在执行npm install --production
并将其仅安装在package.json中dependencies
下列出的依赖项,而dotenv
被列为devDependency
因此未安装。它不是在抱怨找不到您的.env
文件,而是在抱怨它无法解析dotenv
模块。
考虑到dotenv
的作用,它应该是生产依赖性,应该从devDependencies
移到dependencies
。