Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

> [email protected] 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! [email protected] build: `react-scripts build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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.

like image 813
Nishant Kumar Avatar asked Oct 27 '20 05:10

Nishant Kumar


People also ask

How do I fix npm run build error?

To solve the npm ERR! Missing script: "build" error, make sure to add a build command to the scripts object in your package. json file and open your shell or IDE in the root directory of your project before running the npm run build command.

How do you solve this is probably not a problem with npm there is likely additional logging output above?

Instead of just running your usual npm command like npm run start or npm run build , you need to use the node command --max-old-space-size option. The above command will run npm install with 4GB of memory allocated for the process. The increased memory allocation should help you run the process successfully.

Why is npm start giving error?

If you try to run the command from a different directory, it won't find your package. json file and the Missing script: "start" npm error is caused. If you don't have a package. json file, create one by opening your terminal in your project's root directory and running the command npm init -y .

Why my npm is not working?

If your npm is broken: On Mac or Linux, reinstall npm. Windows: If you're on Windows and you have a broken installation, the easiest thing to do is to reinstall node from the official installer (see this note about installing the latest stable version).


1 Answers

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
like image 185
Nishant Kumar Avatar answered Oct 08 '22 20:10

Nishant Kumar