javascriptherokubuilding

I can build locally but my heroku build is failing


In my local environment, the build works properly, and the server works well, but in Heroku, the build fails.

Even 5 days ago, when I did [git push heroku master] with the same code, I succeeded in distributing it was successful. I just don't know why.

My Node Version: v16.14.0

My npm Version: 8.12.1

THis is the message when I run git push heroku master

Enumerating objects: 157, done.
Counting objects: 100% (157/157), done.
Delta compression using up to 8 threads
Compressing objects: 100% (132/132), done.
Writing objects: 100% (139/139), 34.60 KiB | 1.65 MiB/s, done.
Total 139 (delta 64), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/nodejs
remote: -----> Node.js app detected
remote:        
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        
remote: -----> Installing binaries
remote:        engines.node (package.json):  16.x
remote:        engines.npm (package.json):   8.x
remote:        
remote:        Resolving node version 16.x...
remote:        Downloading and installing node 16.15.1...
remote:        Bootstrapping npm 8.x (replacing 8.11.0)...
remote:        npm 8.12.1 installed
remote:        
remote: -----> Restoring cache
remote:        Cached directories were not restored due to a change in version of node, npm, yarn or stack
remote:        Module installation may take longer for this build
remote:        
remote: -----> Installing dependencies
remote:        Installing node modules
remote:        npm ERR! code EUSAGE
remote:        npm ERR! 
remote:        npm ERR! `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
remote:        npm ERR! 
remote:        npm ERR! Invalid: lock file's fs-capacitor@6.2.0 does not satisfy fs-capacitor@2.0.4
remote:        npm ERR! 
remote:        npm ERR! Clean install a project
remote:        npm ERR! 
remote:        npm ERR! Usage:
remote:        npm ERR! npm ci
remote:        npm ERR! 
remote:        npm ERR! Options:
remote:        npm ERR! [--no-audit] [--foreground-scripts] [--ignore-scripts]
remote:        npm ERR! [--script-shell <script-shell>]
remote:        npm ERR! 
remote:        npm ERR! aliases: clean-install, ic, install-clean, isntall-clean
remote:        npm ERR! 
remote:        npm ERR! Run "npm help ci" for more info
remote:        
remote:        npm ERR! A complete log of this run can be found in:
remote:        npm ERR!     /tmp/npmcache.eCi5I/_logs/2022-06-10T15_29_27_934Z-debug-0.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:        If you're stuck, please submit a ticket so we can help:
remote:        https://help.heroku.com/
remote:        
remote:        Love,
remote:        Heroku
remote:        
remote:  !     Push rejected, failed to compile Node.js app.
remote: 
remote:  !     Push failed
remote:  !
remote:  ! ## Warning - The same version of this code has already been built: 1c50da0c5e766307c0583b04cc9e990d1b19088b
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 1c50da0c5e766307c0583b04cc9e990d1b19088b
remote:  ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname>:main
remote:  !
remote:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote: 
remote: Verifying deploy...
remote: 
remote: !       Push rejected to evencafe-backend.
remote: 
To https://git.heroku.com/evencafe-backend.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/evencafe-backend.git'

This is my package.json file

{
  "name": "evencafe-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "preinstall": "npx npm-force-resolutions",
    "dev": "nodemon --exec babel-node src/server.js",
    "migrate": "npx prisma migrate dev",
    "studio": "npx prisma studio",
    "build": "babel src --out-dir build",
    "start": "node build/server"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/evencoding/evencafe-backend.git"
  },
  "author": "Even Coding",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/evencoding/evencafe-backend/issues"
  },
  "homepage": "https://github.com/evencoding/evencafe-backend#readme",
  "dependencies": {
    "@babel/cli": "^7.17.10",
    "@babel/plugin-transform-runtime": "^7.18.2",
    "@babel/runtime": "^7.18.3",
    "@graphql-tools/load-files": "^6.5.3",
    "@graphql-tools/merge": "^8.2.10",
    "@graphql-tools/schema": "^8.3.10",
    "@prisma/client": "^3.8.1",
    "apollo-server": "^2.25.2",
    "apollo-server-express": "^2.25.2",
    "aws-sdk": "^2.1144.0",
    "bcrypt": "^5.0.1",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "graphql": "^15.8.0",
    "jsonwebtoken": "^8.5.1",
    "fs-capacitor": "6.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.16.10",
    "@babel/node": "^7.16.8",
    "@babel/preset-env": "^7.16.10",
    "nodemon": "^2.0.16",
    "prisma": "^3.14.0"
  },
  "resolutions": {
    "fs-capacitor": "6.2.0",
    "graphql-upload": "^11.0.0"
  }
}

Solution

  • I had the exact same issue a few days ago (early June) while deploying my React app on Heroku as well. It seems the problem was caused by the node/npm version update. By default, Heroku uses the latest stable version of node and npm as the building engines, but it might lead to conflicting dependencies if you use a previous version of them.

    So, if it works completely fine locally but this issue happens while deploying it on Heroku, here is the solution:

    Use node --version and npm --version to check out the versions on your local instance, and then add them under engines section in your package.json file, like this (for you it's 16.14.0 and 8.12.1):

    "engines": {
        "node": "16.14.0",
        "npm": "8.12.1"
    },
    

    then redeploy the app on Heroku. Problem should be fixed.