我目前正在Udemy参加Brad和Bassir的MERN堆栈电子商务课程。当我在Heroku部署时,我得到了这个错误。我不知道如何解决这个问题。
这些是我运行heroku logs——tail command
时的日志2021-10-15T17:50:42.000000+00:00 app[api]: Build started by user roopeshsaravanan.dev@gmail.com
2021-10-15T17:53:37.636364+00:00 app[api]: Deploy 90321645 by user roopeshsaravanan.dev@gmail.com
2021-10-15T17:53:37.636364+00:00 app[api]: Release v19 created by user roopeshsaravanan.dev@gmail.com
2021-10-15T17:53:39.000000+00:00 app[api]: Build succeeded
2021-10-15T17:53:43.122703+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-15T17:53:50.219102+00:00 heroku[web.1]: Starting process with command `node backend/server.js`
2021-10-15T17:53:51.386374+00:00 app[web.1]: internal/modules/cjs/loader.js:905
2021-10-15T17:53:51.386389+00:00 app[web.1]: throw err;
2021-10-15T17:53:51.386390+00:00 app[web.1]: ^
2021-10-15T17:53:51.386390+00:00 app[web.1]:
2021-10-15T17:53:51.386390+00:00 app[web.1]: Error: Cannot find module 'nodemon'
2021-10-15T17:53:51.386390+00:00 app[web.1]: Require stack:
2021-10-15T17:53:51.386391+00:00 app[web.1]: - /app/backend/models/userModel.js
2021-10-15T17:53:51.386391+00:00 app[web.1]: - /app/backend/middleware/authMiddleware.js
2021-10-15T17:53:51.386391+00:00 app[web.1]: - /app/backend/routes/productRoutes.js
2021-10-15T17:53:51.386391+00:00 app[web.1]: - /app/backend/server.js
2021-10-15T17:53:51.386392+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2021-10-15T17:53:51.386392+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2021-10-15T17:53:51.386392+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:974:19)
2021-10-15T17:53:51.386393+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:93:18)
2021-10-15T17:53:51.386393+00:00 app[web.1]: at Object.<anonymous> (/app/backend/models/userModel.js:4:19)
2021-10-15T17:53:51.386393+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1085:14)
2021-10-15T17:53:51.386393+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2021-10-15T17:53:51.386394+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:950:32)
2021-10-15T17:53:51.386394+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:790:12)
2021-10-15T17:53:51.386394+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:974:19) {
2021-10-15T17:53:51.386395+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-10-15T17:53:51.386395+00:00 app[web.1]: requireStack: [
2021-10-15T17:53:51.386395+00:00 app[web.1]: '/app/backend/models/userModel.js',
2021-10-15T17:53:51.386395+00:00 app[web.1]: '/app/backend/middleware/authMiddleware.js',
2021-10-15T17:53:51.386396+00:00 app[web.1]: '/app/backend/routes/productRoutes.js',
2021-10-15T17:53:51.386396+00:00 app[web.1]: '/app/backend/server.js'
2021-10-15T17:53:51.386396+00:00 app[web.1]: ]
2021-10-15T17:53:51.386396+00:00 app[web.1]: }
2021-10-15T17:53:51.503588+00:00 heroku[web.1]: Process exited with status 1
2021-10-15T17:53:51.589143+00:00 heroku[web.1]: State changed from starting to crashed
2021-10-15T17:54:14.240609+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=largegreen.herokuapp.com request_id=9000e675-5c48-4ef5-bcd0-9e968668542a fwd="42.111.162.194" dyno= connect= service= status=503 bytes= protocol=https
2021-10-15T17:54:16.260707+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=largegreen.herokuapp.com request_id=d645168d-e122-435d-bd5f-2634f6a66f28 fwd="42.111.162.194" dyno= connect= service= status=503 bytes= protocol=https
2021-10-15T17:54:24.212721+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=largegreen.herokuapp.com request_id=35ab40a6-e42d-4b54-83a1-9b5526929d38 fwd="42.111.162.194" dyno= connect= service= status=503 bytes= protocol=https
2021-10-15T17:54:25.252707+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=largegreen.herokuapp.com request_id=65c7660f-9c13-42af-92f3-f358830bb993 fwd="42.111.162.194" dyno= connect= service= status=503 bytes= protocol=https
2021-10-15T18:16:35.058685+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=largegreen.herokuapp.com request_id=e3380719-6b36-4db6-a705-e1ea7eea88dc fwd="42.111.162.194" dyno= connect= service= status=503 bytes= protocol=https
2021-10-15T18:16:36.046104+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=largegreen.herokuapp.com request_id=6dd2cf7e-ca8c-481a-94b6-70f3ed0d3683 fwd="42.111.162.194" dyno= connect= service= status=503 bytes= protocol=https
1。尝试设置这种类型的目录结构
|__client/ THIS IS EVERYTHING FROM THE REACT SIDE
|__ node_modules/
|__ tons of stuff...
|__ public/
|__ index.html
|__ favicon.ico
|__ etc.
|__ src/
|__ index.js
|__ main/
|__ App.js
|__ etc.
|__ models/
|__ user.js
|__ todo.js
|__ etc.
|__ node_modules/
|__ stuff...
|__ routes
|__ userRoutes.js
|__ todoRoutes.js
|__ etc.
|__ .gitignore
|__ package.json
|__ server.js
|__ etc.
2。添加这个中间件
// ... Other imports
const path = require("path");
// Other app.use middleware
app.use(express.static(path.join(__dirname, "client", "build")));
// Right before your app.listen(), add this:
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(...);
3。向客户端的package.json
添加代理
"proxy": "http://localhost:8000"
那么,您的package.json
文件看起来像…
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.17.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:8000"
}
4。设置网站环境变量:
A. Open your app in Heroku
B. Go to "Settings"
C. Click "Reveal Config Vars"
D. Add a new variable and click "Add". Done!
E. Your port has to look like:- process.env.PORT || 8000
你的端口看起来像这样:-
const port = process.env.PORT || 8000;
.
.
.
app.listen(port);
5。设置MongoDB Atlas IP访问控制
接下来,您需要将IP地址列入白名单,以便可以访问您的集群。在导航菜单中选择"网络接入",选择"添加IP地址"。您可以添加您当前的IP地址或允许从任何地方访问.你的选择。
对于任何通过Heroku部署的项目,您必须选择Allow Access from Anywhere。这是因为Heroku使用动态ip。虽然这不是最安全的选择,但目前还可以。
6。添加start
、heroku-postbuild
和engines
脚本
在这一切之后,你的服务器的package.json
应该看起来像这样:在终端输入node -v
。它应该会显示出你正在使用的Node.js版本。将此版本添加到"engines": "14.17.5"
{
"name": "mern-to-heroku",
"version": "1.0.0",
"main": "server.js",
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"mongoose": "^4.13.6",
"morgan": "^1.9.0"
},
"scripts": {
"start": "node server.js",
"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
},
"engines": {
"node": "14.17.5"
}
}
您现在应该已经设置好了,可以通过Heroku部署您的站点了!
我试着部署自己,得到同样的错误。使端口动态(最可能的是process.env.PORT)),并设置正确的运行命令web: npm run start:prod
来源和更多信息:https://dev.to/rosyshrestha/deploy-nestjs-typescript-app-to-heroku-27e