1

App repo: https://github.com/calebkish/DevConnector

To preface, my web app has been pushed to Heroku.

I'm trying to successfully build my MERN web application onto Heroku, but when I visit my app page (https://safe-fortress-83003.herokuapp.com/), it just returns the error "Not Found" from the root GET response. When I view the logs for my Heroku app, this error displays when a root request is sent:

Error: ENOENT: no such file or directory, stat '/app/client/build/index.html'

So, I went into my local CLI and ran heroku run bash, viewed the current files and directories, and found that /app/client/build/ didn't exist. This leads me to believe that it isn't being generated using my heroku-postbuild script defined in my package.json (no idea why it isn't running when I git push heroku master). Out of desperation, I ran npm install && npm run build inside my client folder (where my react app is located), and now the build folder containing index.html is on my heroku app. When I try to view the app however, I still get an error of "Not Found".

I've been trying to debug this for hours, no idea what is wrong. The static files (build folder) are being defined inside server.js if you'd like to take a look.


EDIT: I just removed build from .gitignore and pushed to heroku, and my app works now. So the main problem is that my heroku-postbuild script isn't running even though the name is clearly defined in this documentation: https://devcenter.heroku.com/articles/nodejs-support

1 Answer 1

0

i have a solution for that particular problem first you create a folder with name of app and put your client folder on it and go for server.js file

if (process.env.NODE_ENV) {
   //static folder add
    app.use(express.static('app/client/build'));
    app.get("*", function (req, res) {
    res.sendFile(path.resolve(__dirname , "app/client/build", "index.html"));
     });
   }

after that you change the package.json file

 "scripts": {
     "start": "node server.js",
     "server": "nodemon server.js",
     "heroku-postbuild": "cd app && cd client && npm install --only=dev && npm 
       install && npm run build"
     },

after that you run your file that works fine

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 2 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy