部署应用程序时出现 Heroku 错误:"错误:找不到模块'/app/index.js'



我想弄清楚如何部署react-express应用程序到Heroku,所以我创建了一个非常简单的应用程序来做到这一点。它是一个具有快速后端的create-react-app前端,在localhost:3000有一个开发服务器,代理api调用到localhost:5000的服务器。然而,每当我试图加载我的应用程序的URL时,我总是得到一个"应用程序错误",在检查错误日志后,我注意到以下错误:

Error: Cannot find module '/app/index.js'
app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:667:27)
app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
app[web.1]: at internal/main/run_main_module.js:17:47 {
app[web.1]: code: 'MODULE_NOT_FOUND',

对于如何解决这个问题有什么建议吗?

这是我的项目结构:

client
+-build
+-node_modules
public
+-index.html
src
+-App.js
+-index.js
package.json
yarn.lock
server
+-models
+-reviews.js
+-node_modules
+-package.json
+-server.js
+-yarn.lock

我是这样配置我的服务器的:

const express = require('express');
const cors = require('cors')
const bodyParser = require('body-parser');
const mongoose = require('mongoose')
const Review = require('./models/reviews')
const path = require('path')
//Server
const app = express();
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Listening on port ${port}`));
//Middleware
app.use(express.static(path.join(__dirname, 'build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded())
app.use(cors())
//Mongo config
const dbURI = 'mongodb+srv://joshydsimon:Josh1985!@mochawelly.8cxdz.mongodb.net/MochaWelly?retryWrites=true&w=majority'
mongoose.connect(dbURI, {useNewUrlParser:true, useUnifiedTopology:true})
.then(() => {
console.log('connected to db')
})
.catch((err) => {
console.log(err)
})
app.get('/api/all-reviews', (req,res) => {
Review.find()
.then((result) => {
res.send(result)
})
.catch(err => {
console.log(err)
})
})
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

最后,这里是我添加到包中的一些脚本。Json(服务器端):

"start": "node index.js",
"heroku-postbuild": "cd client && npm install && npm run build",

它可能没有指向您的客户端文件夹?

//Try this?
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}

也试试这个:

"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently "npm run server" "npm run client"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"}

也试试这个:

app.use(express.static(path.join(__dirname, 'client/build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded())
app.use(cors())

相关内容

  • 没有找到相关文章

最新更新