1

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:[email protected]/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",
1
  • hey, I'm interested in if you find a solution since most tutorials online are for directories where the server and heroku postbuild script are in the main root. Commented Jan 24, 2021 at 22:46

1 Answer 1

1

Its possibly not directing into your client folder?

//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"));
  });
}

Also try this:

"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"}

Also try this:

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())

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.