Search code examples
node.jsgitherokunpmmern

MERN app fails to run "heroku-postbuild" script while being deployed to Heroku


Everything runs just fine until I try to deploy the app to the Heroku. After logging in, these are the terminal commands I run to deploy the app (and the response):

heroku login
heroku: Press any key to open up the browser to login or q to exit: 
Logging in... done
Logged in as #hidden

heroku create
Creating app... done, ⬢ lower-donair-61893
https://lower-donair-61893.herokuapp.com/ | https://git.heroku.com/lower-donair-61893.git

git init
Initialized empty Git repository in #hidden

heroku git:remote -a lower-donair-61893

git add .
warning: adding embedded git repository: client
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached client
hint: 
hint: See "git help submodule" for more information.
warning: LF will be replaced by CRLF in package-lock.json.
The file will have its original line endings in your working directory

git commit -am "make it better"
[master (root-commit) cdaf621] make it better
 13 files changed, 2518 insertions(+)
 create mode 100644 .gitignore
 create mode 160000 client
 create mode 100644 config/default.json
 create mode 100644 models/Comment.js
 create mode 100644 models/Post.js
 create mode 100644 models/User.js
 create mode 100644 package-lock.json
 create mode 100644 package.json
 create mode 100644 routes/api/auth.js
 create mode 100644 routes/api/posts.js
 create mode 100644 routes/api/users.js
 create mode 100644 server.js

The "fun" starts when I run the last command:

git push heroku master
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 8 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (19/19), 28.99 KiB | 1.61 MiB/s, done.
Total 19 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote:        
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 12.x...
remote:        Downloading and installing node 12.18.2...
remote:        Using default npm version: 6.14.5
remote:        
remote: -----> Installing dependencies
remote:        Installing node modules (package.json + package-lock)
remote:        
remote:        > [email protected] postinstall /tmp/build_01ff90de74651f20444f369d014096d8/node_modules/nodemon
remote:        > node bin/postinstall || exit 0
remote:
remote:        Love nodemon? You can now support the project via the open collective:
remote:         > https://opencollective.com/nodemon/donate
remote:
remote:        added 267 packages from 151 contributors and audited 268 packages in 9.402s
remote:        
remote:        12 packages are looking for funding
remote:          run `npm fund` for details
remote:
remote:        found 0 vulnerabilities
remote:
remote:        
remote: -----> Build
remote:        Running heroku-postbuild
remote:        
remote:        > heroku-postbuild /tmp/build_01ff90de74651f20444f369d014096d8
remote:        > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client
remote:
remote:        up to date in 0.219s
remote:        found 0 vulnerabilities
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_01ff90de74651f20444f369d014096d8/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_01ff90de74651f20444f369d014096d8/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.0iNHH/_logs/2020-07-01T13_46_53_504Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 254
remote: npm ERR! 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 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.0iNHH/_logs/2020-07-01T13_46_53_520Z-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 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 lower-donair-61893.
remote:
To https://git.heroku.com/lower-donair-61893.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/lower-donair-61893.git'

So it's an ENOENT, yet I searched for other MERN apps being deployed to Heroku and found this vid: https://www.youtube.com/watch?v=71wSzpLyW9k&list=PLillGF-RfqbbiTGgA77tGO426V3hRF9iE&index=8 in which he used the same lines of code as I did. Also my app is a "continue" of his series, so the file structures, scripts etc. are the exact same (that means if you navigate to the video's desc, you can see the link to the github repo, which has a lot less, but the key files such as package.json etc. are cloned).

This is the postbuild script I'm trying to run:

"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"

This is a screenshot of my file structure: file structure

I've been searching for an answer for past 4 hours and found absolutely nothing. The error messages point out this path, but I can't really find it to review the full log.

/tmp/npmcache.0iNHH/_logs/2020-07-01T13_46_53_504Z-debug.log 

Please, if you need to see more, tell me and I'll happily provide whatever you need - for now I'll copy all the code in both package.json files (server and client):

Server one:

{
    "name": "#hidden",
    "version": "1.0.0",
    "description": "#hidden",
    "main": "server.js",
    "scripts": {
        "client-install": "npm install --prefix client",
        "start": "node server",
        "server": "nodemon server",
        "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"
    },
    "keywords": [
       #hidden
    ],
    "author": "#hidden",
    "license": "ISC",
    "dependencies": {
        "bcryptjs": "^2.4.3",
        "concurrently": "^5.2.0",
        "config": "^3.3.1",
        "express": "^4.17.1",
        "jsonwebtoken": "^8.5.1",
        "mongoose": "^5.9.19"
    },
    "devDependencies": {
        "nodemon": "^2.0.4"
    }
}

Client one:

{
    "name": "client",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "axios": "^0.19.2",
        "bootstrap": "^4.5.0",
        "moment": "^2.27.0",
        "react": "^16.13.1",
        "react-dom": "^16.13.1",
        "react-moment": "^0.9.7",
        "react-redux": "^7.2.0",
        "react-router-dom": "^5.2.0",
        "react-router-redux": "^4.0.8",
        "react-scripts": "3.4.1",
        "react-transition-group": "^4.4.1",
        "reactstrap": "^8.4.1",
        "redux": "^4.0.5",
        "redux-thunk": "^2.3.0"
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    "eslintConfig": {
        "extends": "react-app"
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    },
    "proxy": "http://localhost:5000"
}

Solution

  • I managed to solve this and successfuly deploy the app by removing all the default gitignore exceptions inside my client folder. I don't know why, but there was "/build" included. My final client gitignore file only includes:

    /node_modules