circuit

How to Run 'npm run build' on a Low Spec Server?




Photo by Ferenc Almasi on Unsplash

Photo by Ferenc Almasi on Unsplash

I believe in the world of NPM, you encounter problems that are hard to solve. Has it ever made your head spin trying to execute npm run build on your server or your local environment? For example, like getting the below error message.

Killed
npm ERR! code ELIFECYCLE
npm ERR! errno 137
npm ERR! project-nuxt@1.0.0 build: `nuxt build`
npm ERR! Exit status 137
npm ERR!
npm ERR! Failed at the project-nuxt@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/administrator/.npm/_logs/2021-04-11T08_48_48_675Z-debug.log

If yes, you are not alone. The error message “Exit status 137” refers to a situation when NPM doesn't have enough memory to compile your code in your potato server. What npm run build does is run the script “build” in package.json which normally is a script to compile or build or run your application. So basically, it's a RAM eater, family 😅.

In this article, I'm going to share the solution based on my experience.

What is the solution?

There is a way to overcome this issue which is to limit the maximum memory you want to use to build your package.json.

Go to package.json and see the script

....
"scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate"
},
....

All you need is to specify the limitation memory at the build stage. To avoid error 137, increase memory limit amount by using the argument —-max-old-space-size. It will help to avoid a memory limit issue. For example,

node --max-old-space-size=800 node_modules/nuxt/bin/nuxt.js build
node --max-old-space-size=1000 node_modules/nuxt/bin/nuxt.js build
node --max-old-space-size=VALUE node_modules/nuxt/bin/nuxt.js build
....
....

You can choose any desired value (in MB) and replace it. Copy and put it in your package.json “build” script.

....
"scripts": {
    "dev": "nuxt",
    "build": "PASTE HERE",
    "start": "nuxt start",
    "generate": "nuxt generate"
},
....

After that, just save and npm run build again 🍻.

Everything's going to run smoothly. But you will notice that the script will run slowly because you have allocated a low memory limit for NPM to compile your code.

What if doesn't work?

If the above solution doesn't work for you, please check if there is swap memory available. If not, your need to create a swap file by following the below step:

  1. Check the system swap information by running this command

    # sudo swapon --show
    NAME      TYPE  SIZE USED PRIO
    
    # free -h
                  total    used    free   shared  buff/cache   available
    Mem:          1987     555     991      36         440        1279
    Swap:          0        0        0
    

If you don't get back any output, this means your system does not have swap space available currently. If you already have, there might be some other problem which in all likelihood might be a problem with your code.

  1. Before you create the swap memory, you should check the current disk usage

    # dh -f
    ...
    /dev/vda1        49G  6.0G   43G  13% /
    ...
    
  2. So, based on the above example, there is plenty of space available. Proceed to create a swap file

    # sudo fallocate -l 1G /swapfile
    # sudo chmod 600 /swapfile# sudo mkswap /swapfile
    # sudo swapon /swapfile
    # sudo swapon --show
    # free -h
    

By this point, your swap file is created. Try to rerun “npm run build”. It should be working fine by now. 😁

Great! Now you can happily run your NPM with a smile on your face. Hopefully, this solution will help lower your JavaScript-related stress. Thanks for reading. Happy coding and happy working!

References




Continue Learning