5

I had tried to deploy a MERN stack application on heroku. I had followed the procedure given in the heroku documentation. But, somehow I am unable to deploy the application to heroku.

The following is my server.js code:

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');

const items = require('./routes/api/items');

const app = express();

app.use(bodyParser.json());

const db = require('./config/keys').mongoURI;
mongoose
 .connect(db)
 .then(() => console.log('MongoDB Connected...'))
 .catch(err => console.log(err));


 app.use('/api/items', items);

 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'));
    });
 }

 const port = process.env.PORT || 5000;
 app.listen(port, () => console.log(`Server started on port ${port}`));

The following is my package.json file in the server folder:

{
  "name": "mern_shopping_list",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "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"
},
"author": "",
"license": "ISC",
"dependencies": {
  "body-parser": "^1.19.0",
  "concurrently": "^5.1.0",
  "express": "^4.17.1",
  "mongoose": "^5.9.0"
},
"devDependencies": {
   "nodemon": "^2.0.2"
}
}

The error I am getting is the following:

remote: -----> Build
remote:        Running heroku-postbuild
remote:
remote:        > mern_shopping_list@1.0.0 heroku-postbuild 
/tmp/build_6dafb80a10b85bd553ee344141227145
remote:        > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix 
client
remote:
remote:        up to date in 0.284s
remote:        found 0 vulnerabilities
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_6dafb80a10b85bd553ee344141227145/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open 
'/tmp/build_6dafb80a10b85bd553ee344141227145/client/package.json'
remote: npm ERR! enoent This is related to npm not being able to find a file.
remote: npm ERR! enoent
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /tmp/npmcache.2Rtdv/_logs/2020-02-15T15_35_57_654Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 254
remote: npm ERR! mern_shopping_list@1.0.0 heroku-postbuild: `NPM_CONFIG_PRODUCTION=false npm install 
--prefix client && npm run build --prefix client`
remote: npm ERR! Exit status 254
remote: npm ERR!
remote: npm ERR! Failed at the mern_shopping_list@1.0.0 heroku-postbuild script.
remote: npm ERR! This is probably not a problem with npm. There is likely additional logging output 
above.
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /tmp/npmcache.2Rtdv/_logs/2020-02-15T15_35_57_666Z-debug.log
remote:
remote: -----> Build failed
remote:
remote:        We're sorry this build is failing! You can troubleshoot common issues here:
remote:        https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote:        Some possible problems:
remote:
remote:        - node_modules checked into source control
remote:          https://blog.heroku.com/node-habits-2016#9-only-git-the-important-bits
remote:
remote:        - Node version not specified in package.json
remote:          https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
remote:
remote:        Love,
remote:        Heroku
remote:
remote:  !     Push rejected, failed to compile Node.js app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to damp-anchorage-46764.
remote:
To https://git.heroku.com/damp-anchorage-46764.git
   ! [remote rejected] master -> master (pre-receive hook declined)
   error: failed to push some refs to 'https://git.heroku.com/damp-anchorage-46764.git'

I had tried out some of the links online but none of them is giving the desired result. Can anybody suggest me how to fix this?

max630
  • 8,762
  • 3
  • 30
  • 55
Y SAHITH
  • 160
  • 2
  • 13

5 Answers5

7

cd to your /client remove .git folder

rm -rf .git

also reset git via this command:

git rm --cached . -rf

As Mina Amir mentioned your changes has already tracked by the git inside your client directory and wont be tracked again with your server/main git in your commits

Babak
  • 71
  • 1
  • 6
  • this is wild. I had done this part way before I deployed to Heroku and yet I had to implement your solution again after I attempted to deploy to Heroku and got the same exact error. Anyway it worked, thanks. – Daniel Feb 17 '21 at 17:15
1

I got this problem, when I made a copy of my project and try to deploy, Copy your backend and client without node_modules then try to npm install in your backend and client folders, then try to run your application, in this point I got an error something called "babel-jest" dependency so I installed it and edited it's version to this "^24.8.0" manually in package.json, and I run the npm install in root folder(backend) again ,then I cloud recover the error and run the application,

after that I started to deployment process, I followed below steps it works.

https://www.youtube.com/watch?v=qdoiwouykAg&list=PL8E3OP1AP-EneRlZoCIQ996os2DLegipk&index=3&t=873s

Hima99
  • 11
  • 2
0

try to remove .git folder from /client, because main .git folder in the root does not see /client/package.json and not tracking any changes inside /client. after that commit everything from the beginning in the root folder

Mina Amir
  • 23
  • 7
0

If you are still having this issue,, I faced a similar issue and what solved my problem is simply rename my client folder to "client" to match the name on that path specified. Hope someone finds this helpful and saves time

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 25 '22 at 11:22
0

It happened to me as well. Here is the exact solution:-

The problem is due to two .git folders. There is one .git folder in the root directory but another one in the "client" directory.

To solve this issue, delete both the .git folders and reinitialize git in the root directory using git init after having deleted both .git folders.

(Note that ReactJS automatically created its own .git folder which causes this problem and we have to delete it at all costs. Further in case you are doing a follow-along of Brad Traversy's tutorial, he skipped telling this important part as well. That's a mistake on his part)

MBS
  • 72
  • 7