1. Blog >
  2. Tech
  3. Fix Netlify error “Command failed with exit code 137”
17 August 2024

Fix Netlify error
“Command failed with exit code 137”

Occasionally, a Next.js site is unable to be deployed on Netlify and encounters the error “Command failed with exit code 137” on each new attempt. This often confusing error is generally due to a lack of memory.

But rest assured, we've got 3 solutions to solve the problem.

Understanding error 137: a memory problem

Here's the error you'll probably find in your Netlify deployment logs

1"build.command" failed                                        
2────────────────────────────────────────────────────────────────
3
4  Error message
5  Command failed with exit code 137: npm run build (https://ntl.fyi/exit-code-137)
6
7  Error location
8  In build.command from netlify.toml:
9  npm run build

First and foremost, it's crucial to understand the nature of this beast. Error 137 is, in fact, the sign of a memory shortage during the build process. Netlify, in its wisdom, is signalling that our Next.js site is a little too resource-hungry.

Possible causes: a memory-intensive Next.js website

It's highly likely that your Next.js site was a tad too memory-hungry during its static generation, and therefore its deployment on Netlify. One or more pages of your site could be loading a pharaonic amount of information during the build, some of which was probably not useful for the generation of these pages.

3 solutions to avoid error 137

Faced with this problem, we have identified 3 solutions for you to consider:

  1. Optimizing the data loaded during site generation

  2. Configuring the CPUs allocated to Next.js during deployment

  3. Migration to another PaaS

1. Optimize the amount of data needed for the build

This approach, although potentially time-consuming, can prove beneficial in the long term. It involves rethinking data management, reducing to a minimum the amount of data that is required and loaded to generate your site's pages.

For example, avoid loading your entire blog to display only part of it on the page listing your latest news. Also, check the management of translation data if your site is multilingual: is it provided with only the information needed to generate a page?

Not only could this solve your deployment problem, it will also boost your site's performance for your visitors. Two birds with one stone!

2. Configure the number of CPUs allocated to Next.js

Here's another solution, which we discovered on an assignment for one of our customers. It's quick to set up and extremely effective. Simply limit the number of CPUs allocated to Next.js during deployment to 6.

To do this, add this option to your next.config.js file:

1module.exports = {
2  // Your configuration...
3  experimental: {
4    cpus: 6
5  }
6}

Why 6? This is the maximum number of CPUs allocated during standard package deployments on Netlify.

3. Migrating to another deployment platform

If the first two solutions don't bear fruit, it may be wise to consider migrating to another deployment platform, such as Vercel, which is more generous in terms of memory capacity during deployments.

Our expertise for your project

This deployment error should no longer be an insurmountable obstacle for you. You should probably be able to find a solution among these three.

But if you're feeling overwhelmed, our team of experts is here to help. Whether you're looking to resolve problems with your Next.js website, optimize your Netlify or Vercel deployments, or even take on more ambitious developments, don't hesitate to call on our services.

Pivot
Warner Bros
Libeo
Swile
Pivot
Warner Bros
Libeo
Swile
Pivot
Warner Bros
Libeo
Swile
Pivot
Warner Bros
Libeo
Swile
  • Vercel usage
    Tech
    Reducing Vercel usage: mastering Data Cache and Function execution

    You're staring at your Vercel invoice in amazement, aren't you? Rest assured, you're not alone in the face of soaring metrics such as “Data Cache”, “Function Duration” and “ISR”.

    We've put together a Next.js optimization guide to help you reduce your Vercel usage without sacrificing performance. We'll let you in on all our secrets for otpimizing your costs.