23

When I tried to push my nodejs app to heroku with git push heroku master, i got this:

Counting objects: 975, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (862/862), done.
Writing objects: 100% (975/975), 3.74 MiB | 80.00 KiB/s, done.
Total 975 (delta 70), reused 0 (delta 0)

-----> Node.js app detected
-----> Resolving engine versions
   Using Node.js version: 0.10.15
   Using npm version: 1.3.3
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
   npm ERR! install Couldn't read dependencies
!     Push rejected, failed to compile Node.js app

To [email protected]:hidden-reaches-9268.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:hidden-reaches-9268.git'

And this is my package.json:

{
  "name": "fnBoard",
  "version": "0.0.1",
  "private": true,
  "scripts": {
  "start": "node server.js"
},

  "dependencies": {
   "socket.io": "0.9.x"
},
   "engines": {
     "node": "0.10.x",
     "npm": "1.3.x"
   }
}

There's a bunch of error inside and I have no idea why this happen. please help. -thanks

7
  • So your package.json is in the root?
    – chromedude
    Sep 6, 2013 at 15:21
  • yes, I have the Procfile as well
    – Pixeladed
    Sep 6, 2013 at 15:22
  • I'd assume you already did npm install locally and it installed the dependencies without a problem?
    – chromedude
    Sep 6, 2013 at 15:24
  • yes all node_module are installed in a folder /node_modules
    – Pixeladed
    Sep 6, 2013 at 15:25
  • I dont know if you have to add it to heroku using commit
    – Pixeladed
    Sep 6, 2013 at 15:25

13 Answers 13

25

I am working in ReactJS and I am trying to deploy my project on Heroku server. At that time I have found same error like this:

Push rejected, failed to compile Node.js app.

enter image description here

Solution is:

If you use yarn:

git rm yarn.lock

git push heroku master

If you use npm:

git rm package-lock.json

git push heroku master

6
  • 10
    and don't forget to git commit -m "removed yarn/npm" before you push and after you rm
    – Ronen
    Jul 12, 2018 at 9:45
  • 4
    This saved my butt. Thank you. Jan 10, 2019 at 23:38
  • I have not staged package-lock.json and it does not work, either Jul 18, 2019 at 22:08
  • 1
    Didn't work for me - still getting the same error message.
    – nelion
    Dec 12, 2019 at 20:48
  • git rm -f yarn.lock to remove forcely.
    – claudios
    Apr 9, 2020 at 14:01
13

The easiest way to make this work is to add node_modules to your .gitignore. Lots more info here: Fail to deploy node.js application to heroku

4
  • where is the .gitignore folder?
    – Pixeladed
    Sep 7, 2013 at 12:16
  • Create a text file at the root level called .gitignore with the contents node_modules.
    – Dan Kohn
    Sep 7, 2013 at 15:48
  • .gitignore.txt or just.gitignore?
    – Pixeladed
    Sep 8, 2013 at 1:01
  • I have not added node_modules to stage and it does not work Jul 18, 2019 at 22:07
7

Adding node_modules may be easy but not the correct approach here. Instead do git push -f heroku master in order to FORCE push your updates telling heroku to overwrite any pre-existing node_modules. This way your git repo is not bogged down with node libs.

1
  • This. I just now noticed the "Caching build: -node_modules" log output from heroku. Once I did a force push it worked for me. Thank you
    – Matteo
    Jan 11, 2022 at 17:09
3

Try setting a heroku-postbuild script to your package.json and make sure to include your engines.

"scripts": {
        "heroku-postbuild": "npm run build"
    },
"engines": {
        "npm": "5.6.0",
        "node": "8.10.0"
      }

I would try and avoid force pushing anything at all costs whether it be to github or heroku.

2
  1. git rm package-lock.json
  2. npm init -y
  3. npm install
  4. git add .
  5. git commit -m "changes"
  6. git push heroku main
1

I solved this.
I got the same error:

"Push rejected, failed to compile Node.js app"

but my log was complaining about this Unknown option:

'--target'

I solved this digging out on my package.json and I found this line of code below:

"postinstall": "ng build --aot --target=production"

I removed the --target=production.

On my terminal:
I commited again $ git commit -m 'anything here'
then $ git push heroku master
And done.

1

I fixed this by just removing the package lock.json and then again git add . and git commit after removing the lock file.

1

Was facing the same issue, however Yogesh's answer did not work for me. The app was running in my local however heroku failed to install dependencies. Noticed that I was using node 16.4.0 in local while heroku used 16.15.1. So I specified my local version in package.json, committed the changes and pushed to heroku and it started working.

    {
        "name": "leetboard-frontend",
        "version": "0.1.0",
        "private": true,
        "engines": {
            "node": "16.4.0"
        },

        .
        .
        .
    }

0

I had the same issue , the problem was with git add. I had forgotten to add the node_modules files. I closed the terminal and ran the set of commands given in the Getting started with Heroku and NodeJs[1] again. The application was successfully pushed onto the stack.

0

Issue When you use Yarn to install node modules, it generates a yarn.lock file which contains a list of the exact modules that it installed when you ran the command. If the dependencies in package.json change, but a new yarn.lock file is not generated by the Yarn executable we fail the build to prevent subtle bugs and security issues that could affect your application at runtime.

Resolution This issue commonly occurs when your application uses Yarn but some other tool modifies the package.json file without invoking yarn install. An example would be using npm to install a new module instead of Yarn, or manually updating a version requirement by hand.

To resolve this, run yarn install and check in the updated yarn.lock file.

ensure you have removed package.lock file and commited the same before push.

0

I fixed it by join to the path of an imported module that did not belong to Node. More info here https://stackoverflow.com/a/64772154/12982656

1
  • Answers must be self contained, & exact code necessary to solve a coding. issues must be embedded. See [help pages](StackOverflow.com/help) for more info. Links can be included, though Link only suggestions are not considered answers on SO either. Both are welcomed and appreciated as Comments. You can edit to include exact code, or repost as a Comment (once you have enough rep). Nov 10, 2020 at 16:31
0

I was getting this error when pushing a Frontity app to Heroku. I also couldn't get the app to compile on my machine locally even after clearing my npm cache, deleting node_modules, etc..

What worked for me was using this Node version manager to change my local Node version from 18.0.0 to 16.10.0.

sudo n 16.10.0

Then updating the Node version in package.json to the same version:

"engines": {
  "node": "16.10.0",
  "npm": ">=6.0.0"
},
0

The following worked for me:

  1. Add the engines in your package.json. Try to match the node version with the one on your machine (you can check by running node -v in your terminal).

    "engines": {
    "node": "16.x"
    }
    
  2. Delete node_modules folder and package-lock.json

  3. Run npm install

  4. Make sure you are using the right buildpack. Run the following code, if your app is already using the correct buildpack it will tell you.

    heroku buildpacks:set heroku/nodejs
    
  5. You might be including node_modules in your source code. To find out run the following:

    git ls-files | grep node_modules
    
  6. If you get any results back, run the following to stop tracking node_modules:

    echo "node_modules" >> .gitignore
    echo "/lib" >> .gitignore
    git rm -r --cached node_modules
    git commit -am 'untracked node_modules'
    
  7. Finally, git add and commit your changes.

  8. Try pushing to Heroku again. Good luck and hopefully this helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.