Search code examples
node.jsreactjsdevopsweb-deploymentdigital-ocean

npm run build works fine on local machine but shows error on remote ubuntu server


I'm trying to deploy my MERN app on the digital ocean remote ubuntu server. After git clone, I did npm install to my root folder, client folder, and server folder. But when I tried to run npm start from the root folder then only the server app was running and an error came on the client-side. So I did cd into the client folder and tried the command npm run build (which I did on my local machine as well and the optimized build got created successfully) but it showed the below error on the remote server

> client@0.1.0 build /home/nishant/apps/rentaporta/client
> react-scripts build

Creating an optimized production build...
The build failed because the process exited too early. This probably means the system ran out of memory or someone called `kill -9` on the process.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! client@0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the client@0.1.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/nishant/.npm/_logs/2020-10-27T05_22_30_755Z-debug.log

I deleted node_modules, package-lock.json, and tried the npm ci command as well but there was no improvement. My folder structure is

root
 client
 server

Below is my package.json script in root folder

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "client-install": "npm install --prefix client",
    "server-install": "npm install ---prefix server",
    "server": "npm start --prefix server",
    "client": "npm start --prefix client",
    "build-client": "npm run build --prefix client",
    "dev": "concurrently \"npm run server\"  \"npm run client\"",
    "start": "npm run server-install & concurrently \"npm run build-client\"  \"npm run server\""
  },

Please, someone, help me. If you need more explanation I'm ready to put more details as needed.


Solution

  • finally, I figured out the problem. My remote ubuntu server had 1GB ram but there was no swap memory. I used the command sudo free -h, and found swap memory was 0. So, I followed this article (https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04) to create the swap memory, and finally, my app got deployed.

    Also, cd into your client folder and run this command in the terminal-

    $ export NODE_OPTIONS=--max-old-space-size=8192