I’m trying to figure out how to deploy react-express apps to Heroku, so I’ve created a very simple app to do this. Its’ a create-react-app
front end with a express backend, with a dev server at localhost:3000 that proxies api calls to the server at localhost:5000.
However, I keep getting an ‘Application error’ whenever I try to load my app’s URL, and after checking the error logs, I noticed the following error:
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',
Any tips on how to solve this?
Here’s my project structure:
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
And here’s how I’ve configured my server:
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'));
});
Lastly, here’s some scripts I added to the package.json (server side):
"start": "node index.js",
"heroku-postbuild": "cd client && npm install && npm run build",