我正在尝试在Heroku上部署MERN应用程序,但是在成功构建之后会出现一些错误。
总是这样:无法查找模块'xxx'
我的应用程序分为两个文件夹:服务器文件夹(API节点)和客户端文件夹(REACT)
我的package.json在根文件夹中:
...
"scripts": {
"start": "cd ./server && node server.js",
"heroku-postbuild": "cd ./client && yarn && yarn build"
},
...
我在根文件夹中的procfile:
web: cd ./server && node server.js
服务器文件夹中的我的server.js文件:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const passport = require('passport');
const users = require('./routes/api/users');
const favs = require('./routes/api/favs');
const path = require('path');
require('dotenv').config();
// Serve static files from the React app
app.use(express.static(path.join(__dirname, '../client/build')));
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(
cors(),
bodyParser.json()
);
mongoose.connect(
`mongodb+srv://admin:${process.env.DB_PASS}@cluster0.o5plw.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`,
{ useNewUrlParser: true,
useUnifiedTopology: true
}
).then(() => console.log("MongoDB connected"))
.catch((err) => console.log("MongoDB :", err));
app.use(passport.initialize());
require('./config/passport')(passport);
app.use("/api/users", users);
app.use("/api", favs);
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
app.listen(PORT, () => {
console.log(`Listenning on ${PORT}`);
});
我已经尝试过:
$ heroku config:set NODE_MODULES_CACHE=false
$ git commit -am 'disable_node_modules_cache' --allow-empty
$ git push heroku master
有人会有想法吗?
提前谢谢