'How can I deploy nextjs and nodejs on Heroku without using npm run build on the client/frontend side?

So, I don't want to build my nextjs since it will break the code for connecting to the backend. I'm not sure if you get it, and that is what it is... If I launch heroku with npm run build, the frontend and graphql function well, but it doesn't connect to the backend...

here is my package.json:

{
  ...
  "scripts": {
    "f": "next dev",
    "backend": "cd backend && npm run s --prefix backend",
    "build": "next build",
    "start": "next start",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix backend && npm run build"
  },
  ...
  ...
  "engines": {
    "node": "14.x",
    "npm": "7.16.0"
}

my project structure is nextjs project > backend folder > backend files

When I delete "npm run build" from the heroku-postbuild scripts, thethe website no longer displays the client side and instead displays an error stating "heroku tail logs" something... I have many websites hosted on nginx vps making nextjs and nodejs project, but I wanted to use heroku because this website isn't really worth the vps fee.

Is it possible to do this?

Details:

  • Im on mac
  • I installed heroku using brew in terminal
  • I ran buildpacks for heroku/nodejs
  • I used the method of heroku cli
  • I followed the git instructions properly
  • My port is 5001 in backend
  • When i go to the website-url:5001/graphql it gives me error... and yes ive changed all codes to the website url and not localhost


Solution 1:[1]

try using docker, export one port for backend and another for frontend. you can use nginx which will run on the top of linux image

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanishk