0
votes

I'm trying to do a production build on Heroku of a udemy course React project. I've tried so many things to make it work but I'm completely at a loss.

The output from the Heroku build process:

-----> Node.js app detected

-----> Creating runtime environment

   NPM_CONFIG_LOGLEVEL=error
   NODE_ENV=production
   NODE_MODULES_CACHE=true
   NODE_VERBOSE=false

-----> Installing binaries engines.node (package.json): 12.17.0 engines.npm (package.json): 6.14.5

   Resolving node version 12.17.0...
   Downloading and installing node 12.17.0...
   Bootstrapping npm 6.14.5 (replacing 6.14.4)...
   npm 6.14.5 installed

-----> Restoring cache Cached directories were not restored due to a change in version of node, npm, yarn or stack Module installation may take longer for this build

-----> Installing dependencies Installing node modules (package.json)

   > nodemon@2.0.4 postinstall /tmp/build_01409f5f7cae8788b3a2ccca28c521d8/node_modules/nodemon
   > node bin/postinstall || exit 0

   Love nodemon? You can now support the project via the open collective:
    > https://opencollective.com/nodemon/donate

   added 267 packages from 183 contributors and audited 268 packages in 11.725s

   12 packages are looking for funding
     run `npm fund` for details

   found 0 vulnerabilities

-----> Build Running heroku-postbuild

   > server@1.0.0 heroku-postbuild /tmp/build_01409f5f7cae8788b3a2ccca28c521d8
   > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client

   up to date in 0.618s
   found 0 vulnerabilities
    npm ERR! code ENOENT npm ERR! syscall open npm ERR! path /tmp/build_01409f5f7cae8788b3a2ccca28c521d8/client/package.json npm

ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_01409f5f7cae8788b3a2ccca28c521d8/client/package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! /tmp/npmcache.h4E00/_logs/2020-05-27T18_18_35_286Z-debug.log npm ERR! code ELIFECYCLE npm ERR! errno 254 npm ERR! server@1.0.0 heroku-postbuild: NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client npm ERR! Exit status 254 npm ERR! npm ERR! Failed at the server@1.0.0 heroku-postbuild script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR!
/tmp/npmcache.h4E00/_logs/2020-05-27T18_18_35_303Z-debug.log -----> Build failed

   We're sorry this build is failing! You can troubleshoot common issues here:
   https://devcenter.heroku.com/articles/troubleshooting-node-deploys

   If you're stuck, please submit a ticket so we can help:
   https://help.heroku.com/

   Love,
   Heroku
     !     Push rejected, failed to compile Node.js app.  !     Push failed

From the server index.js:

if (process.env.NODE_ENV === 'production') {
  // Express will serve production assets
  app.use(express.static('client/build'));

  // Express will serve index.html file if it doesn't recognise the route
  const path = require('path');
  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', "index.html"));
  });
}

The server package.json:

{
    "name": "server",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "engines": {
        "node": "12.17.0",
        "npm": "6.14.5"
    },
    "scripts": {
        "start": "node index.js",
        "server": "nodemon index.js",
        "client": "npm run 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"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "body-parser": "^1.19.0",
        "concurrently": "^5.2.0",
        "cookie-session": "^1.4.0",
        "express": "^4.17.1",
        "mongoose": "^5.9.15",
        "nodemon": "^2.0.4",
        "passport": "^0.4.1",
        "passport-google-oauth20": "^2.0.0",
        "stripe": "^8.55.0"
    }
}

.gitignore:

node_modules
dev.js
2

2 Answers

0
votes

It turns out that because I hadn't deleted the .git created by create-react-app in my /client directory, this was stopping the /server repository including the front-end files. So just had to delete this and reinitialise the /server git repository.

0
votes

You have to delete .git file that created by create-react-app try this: first in main app folder delete .git file watch out for hiden folders then go to client and search for .git in there if there was any delete it in terminal go to your project folder and type git init then follow the steps that tells you on your heroku main dashboard page