1

I want to automate the deployment of a react app on my server. I have a github webhook event that hits a python Flask endpoint where I then run npm run build:stage in a subprocess.

If I run the build command in an ssh login session, it works fine and the build generates but when the webhook fires and the command is run through the python subprocess, I see the following error:

Python code that executes the command:

p = subprocess.Popen(command,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
09/05/2020 02:42:59 PM +08 : INFO : Process Output: 
> [email protected] build:stage /home/user/reactapp
> env-cmd -f .env.prod react-scripts build

Creating an optimized production build...

<--- Last few GCs --->

[5541:0x2bc1760]    19190 ms: Scavenge 524.4 (551.5) -> 516.9 (554.2) MB, 10.4 / 0.0 ms  (average mu = 0.989, current mu = 0.988) allocation failure 
[5541:0x2bc1760]    19217 ms: Scavenge 528.1 (555.1) -> 528.0 (559.4) MB, 16.6 / 0.0 ms  (average mu = 0.989, current mu = 0.988) allocation failure 
[5541:0x2bc1760]    19432 ms: Scavenge 533.1 (560.0) -> 532.7 (563.7) MB, 209.0 / 0.0 ms  (average mu = 0.989, current mu = 0.988) allocation failure 


<--- JS stacktrace --->
Cannot get stack trace in GC.

09/05/2020 02:42:59 PM +08 : ERROR : Process Error: FATAL ERROR: Scavenger: semi-space copy Allocation failed - JavaScript heap out of memory
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build:stage: `env-cmd -f .env.prod react-scripts build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build:stage 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/user/.npm/_logs/2020-09-05T06_42_59_795Z-debug.log

After going through Link 1, Link 2, Link 3

I added the following to my bashrc
export NODE_OPTIONS=--max_old_space_size=4096 --max-semi-space-size=4096000

and updated my build command to:
"build:stage": "env-cmd -f .env.prod react-scripts --max_old_space_size=4096 --max-semi-space-size=4096000 build"

The error changed to:

09/05/2020 03:10:16 PM +08 : INFO : Process Output:
> [email protected] build:stage /home/user/reactapp
> env-cmd -f .env.prod react-scripts --max_old_space_size=4096 --max-semi-space-size=4096000 build

Creating an optimized production build...

<--- Last few GCs --->

[7923:0x2e1d950]    33699 ms: Scavenge 415.5 (451.4) -> 412.5 (535.7) MB, 37.8 / 0.0 ms  (average mu = 0.858, current mu = 0.000) allocation failure 
[7923:0x2e1d950]    33913 ms: Scavenge 474.0 (542.3) -> 438.2 (546.6) MB, 36.2 / 0.0 ms  (average mu = 0.858, current mu = 0.000) allocation failure 
[7923:0x2e1d950]    34236 ms: Scavenge 480.8 (549.1) -> 454.3 (567.2) MB, 77.7 / 0.0 ms  (average mu = 0.858, current mu = 0.000) allocation failure 


<--- JS stacktrace --->
Cannot get stack trace in GC.

09/05/2020 03:10:16 PM +08 : ERROR : Process Error: FATAL ERROR: NewSpace::Rebalance Allocation failed - JavaScript heap out of memory
 1: 0x9d8da0 node::Abort() [node]
 2: 0x9d9f56 node::OnFatalError(char const*, char const*) [node]
 3: 0xb37dbe v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xb38139 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xce34f5  [node]
 6: 0xd2a8ae  [node]
 7: 0xd2e627 v8::internal::MarkCompactCollector::CollectGarbage() [node]
 8: 0xcef049 v8::internal::Heap::MarkCompact() [node]
 9: 0xcefdb3 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
10: 0xcf0925 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
11: 0xcf1fcf v8::internal::Heap::HandleGCRequest() [node]
12: 0xca0fb4 v8::internal::StackGuard::HandleInterrupts() [node]
13: 0xfef887 v8::internal::Runtime_StackGuard(int, unsigned long*, v8::internal::Isolate*) [node]
14: 0x13725d9  [node]
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build:stage: `env-cmd -f .env.prod react-scripts --max_old_space_size=4096 --max-semi-space-size=4096000 build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build:stage 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/user/.npm/_logs/2020-09-05T07_10_16_341Z-debug.log

# npm -v
6.12.0
# node -v
v12.13.0

Server: CentOS Linux release 7.6.1810 (Core)

No clue how to proceed further

UPDATE:

I upgraded my hosting plan to increase the RAM from 4GB to 6GB. Now if I run the python snipper in a python console, the build generates fine but when it runs via the webhook throught flask app, I still see it fail with the above error.

1
  • Ultimately, I was unable to get this to work and moved our react app hosting to the vercel.com Commented Mar 3, 2021 at 13:29

1 Answer 1

1

I had exactly the same problem running "npm audit fix" via python subprocess.run. I got it fixed via moving this part into a script and executing the shell (bash in my case) with the script from within my Python script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.