Firmin Martin
Firmin Martin

Reputation: 308

Reduce heroku slug size while whitelisting 'serve' and its dependencies

I have a React app (CRA) that exceeds heroku's maximum slug size soft limit (300MB), so I use this solution to reduce the slug size:

  1. heroku buildpacks:add https://github.com/opencounter/heroku-buildpack-post-build-clean.git
  2. Add the file .slug-post-clean at the project root:
src
public
node_modules

But, as the same time, I'm serving my app using serve, package.json:

...
"dependencies": {
...
  "serve": "^12.0.1",
...
}, 
...
"scripts" : {
  "start": "serve -s build",
}

Excluding node_modules altogether will also exclude serve, making heroku unable to serve my app.

How to reduce heroku slug size while whitelisting 'serve' and its dependencies ?

For now, I'm excluding some big independent packages (e.g. @mui, @testing-library, etc.), but that's a bit hacky.

Upvotes: 0

Views: 564

Answers (2)

ReZ
ReZ

Reputation: 391

Try to use heroku-cleanup command in your package.json like this:

script: {
   "start": "serve -s build",
   "heroku-cleanup": "rm -rf node_modules src"
}

It's not necessary any buildpack, you can see more information in heroku support page: https://devcenter.heroku.com/articles/nodejs-support

Upvotes: 1

Firmin Martin
Firmin Martin

Reputation: 308

After excluding node-modules/.cache, the slug size has been reduced down 90% (~400M). So the solution is quite simple:

In .slug-post-clean, add:

node-modules/.cache

Upvotes: 0

Related Questions