Auto start corresponding to "node-red-pi --max-old-space-size=256"

Hi,

My Raspberry Pi doesn't like the "node-red-start" command line (provides errors), but is OK with "node-red-pi --max-old-space-size=256" command line.

Therefore, I would like to know what is the corresponding command line for an autostart of the service with this option ?

Thanks for your support

For auto-start, you should have a systemd service script. I think that Dave's installer script sets that up so you can look in that. You could also look in my alternate installer repo which has an example script file with some extended features.

By using systemd, node-red is started at bootup and restarted on fail. You can manually restart with a command too ( the example package.json file in my alt installer should have the commands listed I think).

And the max-old-space may not be needed with newer versions of node.js so you may wish to try without first.

Did you install it using the recommended install script?

Yes I followed this one

Not working without:

node-red-start

Start Node-RED

Once Node-RED has started, point a browser at http://192.168.1.98:1880
On Pi Node-RED works better with the Firefox or Chrome browser

Use node-red-stop to stop Node-RED
Use node-red-start to start Node-RED again
Use node-red-log to view the recent log output
Use sudo systemctl enable nodered.service to autostart Node-RED at every boot
Use sudo systemctl disable nodered.service to disable autostart on boot

To find more nodes and example flows - go to http://flows.nodered.org

Starting as a systemd service.
<--- Last few GCs --->
[3067:0x5971f70] 68 ms: Mark-sweep 0.3 (2.8) -> 0.3 (2.8) MB, 1.3 / 0.0 ms (average mu = 0.254, current mu = 0.026) allocation failure GC in old space requested
[3067:0x5971f70] 70 ms: Mark-sweep (reduce) 0.3 (2.8) -> 0.3 (1.8) MB, 1.5 / 0.0 ms (average mu = 0.151, current mu = 0.023) last resort GC in old space requested
[3067:0x5971f70] 71 ms: Mark-sweep (reduce) 0.3 (1.8) -> 0.3 (1.8) MB, 1.2 / 0.0 ms (average mu = 0.097, current mu = 0.022) last resort GC in old space requested
<--- JS stacktrace --->
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
nodered.service: Main process exited, code=killed, status=6/ABRT
nodered.service: Failed with result 'signal'.

That is unusual. What does the command
node -v
show?
Can you post the file /var/log/nodered-install.log please. If that file is empty then try /var/log/nodered-install.log.1

Also what happens if you just run the command
node-red

And there is the answer to the question about auto-starting.

Yes, I've never seen NR fail so early on with a heap allocation failure.

Can you add the following code early in the settings.js file? (Must be outside the export object).

/** Optionally display the Node.js/v8 engine heap size on startup */
const v8 = require('v8')
console.info(`V8 Total Heap Size: ${(v8.getHeapStatistics().total_available_size / 1024 / 1024).toFixed(2)} MB`)
let mem = process.memoryUsage()
const formatMem = (m) => ( m/1048576 ).toFixed(2)
console.info(`Initial Memory Use (MB): RSS=${formatMem(mem.rss)}. Heap: Used=${formatMem(mem.heapUsed)}, Tot=${formatMem(mem.heapTotal)}. Ext C++=${formatMem(mem.external)}`)

Then report the output (will be in the node-red log).

Run sudo nano /lib/systemd/system/nodered.service

Verify that these lines are present, if not then add them:

Environment="NODE_OPTIONS=--max_old_space_size=256"
Environment="PI_NODE_OPTIONS=--max_old_space_size=256"

Then run

sudo systemctl daemon-reload
node-red-stop
node-red-start

You can also run sudo systemctl enable nodered.service to auto-start every time it boots.

1 Like

If the first one is not there then we need to know why, hence my request for the other information. Has the second one been added to the script recently? It is not present on my Pis.

On my Pi 4 I only have

Environment="NODE_OPTIONS=--max_old_space_size=2048"

But my Pi 0 W's had this by default

Environment="PI_NODE_OPTIONS=--max_old_space_size=256"

So I manually edited them to include the first line.

Did those come from the install script?

Yes, Node-RED is installed on all of them using the officially provided install script. However, due to Node no longer supporting armv6 hardware, I have installed an unofficial build of Node v16.11.0 with my own script (I am unaware if this may have an effect on it) for the Pi 0 W's.

The script should cope with armv6 by downloading nodejs from an alternative source.

The service file comes from here I believe, and the space setting is adjusted based on the amount of memory available. Perhaps that calculation has gone wrong. @Jeff2 could you copy/paste the service file please so we can have a look at it?

@HaroldPetersInskipp, I don't know where your PI_NODE_OPTIONS entry has come from.

Note that if you re-run the script it will overwrite your changes to the service file. If you want to amend something in an installed service file it is better to use the systemd override feature to do that, then the mods will not get removed if you rerun the script. Look up systemctl edit for how to do that.

It came from me manually editing it in. I believe the install script warns the user and leaves the service file alone if it detects it has been edited.

Basically what I'm trying to say is that my installs on my Pi 0 W's may be unconventional, and should not be solely relied upon for debugging a installation issue.

Where is that env var (PI_NODE_OPTIONS) used?

I don't know tbh, I think that line was already there, as it is referenced here: Customising the Raspberry Pi service : Node-RED

I only added the top line Environment="NODE_OPTIONS=--max_old_space_size=256"

I think node-red-start uses one and node-red-pi uses the other?

I think there is an error in that page, if you look at the first and third boxes you will see that in the first it is called PI_NODE_OPTIONS and in the third the same line has NODE_OPTIONS. I suspect that the former may be from a very old version of the file. I am fairly confident that the only one that matters is NODE_OPTIONS.

I thought that might be the case, so for redundancy I just put both (assuming it wouldn't hurt anything to have an extra variable).

(docs now fixed)