我在Heroku部署了一个MERN应用程序。
我跑了";npm运行构建";并将所有前端文件添加到backend/public/文件夹中。
它在推送它之后启动服务器,因为它给我记录了一个";服务器正在运行";控制台中的消息,但当我访问网站时(https://stonkappmern.herokuapp.com/)它给了我一个应用程序错误(在localhost上运行良好)。
由于某种原因,它无法在终端中处理此错误(在运行"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");
};
这是我的文件夹结构:
在此处输入图像描述
有什么想法吗?我应该上传更多的文件吗?
我认为您的scripts
不正确
https://docs.npmjs.com/cli/v8/using-npm/scripts#npm-开始
节点js部分:
在package.json
中更改scripts
,如下所示:
"scripts": {
"start": "node index.js -p $PORT",
},
heroku查找应用程序的run
的start
命令,并将为$PORT
分配一个端口
根据您的项目结构,您的应用程序根本不需要build
。