Failing to add/start Node Red in PM2

I used to start NR with PM2, but had to reinstall everything after a SSD crash. I followed the tuorials, but its not working. I am running Win11, installed NR via npm. I had NR in PM2, but it didnt start. I deleted it to re add it. But its not starting or adding. The file paths are correct I believe.

C:\Windows\System32>pm2 start red.js --node-red

  error: unknown option `--node-red'


C:\Windows\System32>pm2 start red.js
[PM2][ERROR] Script not found: C:\Windows\System32\red.js

C:\Windows\System32>pm2 start C:\Users\lenov\AppData\Roaming\npm\node-modules\node-red\red.js --node-red

  error: unknown option `--node-red'


C:\Windows\System32>pm2 start C:\Users\lenov\AppData\Roaming\npm\node-modules\node-red\red.js
[PM2][ERROR] Script not found: C:\Users\lenov\AppData\Roaming\npm\node-modules\node-red\red.js

C:\Windows\System32>pm2 start 'C:\Users\lenov\AppData\Roaming\npm\node-red' -- -v
[PM2][ERROR] Script not found: C:\Windows\System32\'C:\Users\lenov\AppData\Roaming\npm\node-red'

C:\Windows\System32>pm2 start 'C:\Users\lenov\AppData\Roaming\npm\node-red.cmd' -- -v
[PM2][ERROR] Script not found: C:\Windows\System32\'C:\Users\lenov\AppData\Roaming\npm\node-red.cmd'

C:\Windows\System32>pm2 start 'C:\Users\lenov\AppData\Roaming\npm\node_modules\node-red\red.js' -- -v
[PM2][ERROR] Script not found: C:\Windows\System32\'C:\Users\lenov\AppData\Roaming\npm\node_modules\node-red\red.js

--node-red is no an option that exists. If you installed node-red globally you'll want pm2 start node-red.

I tried that. It adds NR to PM2, but it wont start

C:\Windows\System32>pm2 start node-red
[PM2] Starting C:\USERS\LENOV\APPDATA\ROAMING\NPM\NODE-RED.CMD in fork_mode (1 instance)
[PM2] Done.
┌────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name        │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ node-red    │ default     │ N/A     │ fork    │ 22256    │ 0      │ 1    │ stopped   │ 0%       │ 0b       │ admin    │ disabled │
└────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

C:\Windows\System32>

C:\Windows\System32>pm2 start 0
[PM2] Applying action restartProcessId on app [0](ids: [ '0' ])
[PM2] [node-red](0) ✓
[PM2] Process successfully started
┌────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name        │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ node-red    │ default     │ N/A     │ fork    │ 5096     │ 0      │ 16   │ stopped   │ 0%       │ 0b       │ admin    │ disabled │
└────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

We can't tell what is wrong if you don't share the log.

I finally found the log

node-red-error.log (92.1 KB)

pm2.log (35.3 KB)

And the log gives you the answer as expected.

SyntaxError: Invalid or unexpected token
...
C:\USERS\LENOV\APPDATA\ROAMING\NPM\NODE-RED.CMD:1
@ECHO off
^

Which implies that you are trying to run a Windows Batch file (.cmd) with something other than the Windows command shell (probably node.js at a guess).

Which almost certainly means that your startup command for PM2/Node-RED is incorrect.

Ok. So, (for a person who does not know pm2 and doesnt understand node.js either), how would I add NR to pm2, and how do I get it to start ? I know how to delete NR of pm2, I did that before. My pm2 is autostarting too, so its 'just' adding NR...

I don't know how you have things configured so I can't really say, I can only say how I have things configured and I don't auto-start my windows instance as I use it for development and it doesn't always need to run. Also, I install node-red in a specific way that may not be the way you've done it.

The documentation for PM2 is pretty good I seem to remember.

I have a PM2 config ecosystem.config.js in the root folder of where I have node-red installed.

// PM2 Configuration for running Node-RED test/dev

module.exports = {
    apps : [{
        name: 'Node-RED',
        script: 'D:/src/nr/node_modules/node-red/red.js',

        // Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
        args: '--userDir ./data',
        watch: [
            // a bunch of watched folders/files that restart node-red if changed
        ],
        ignore_watch: [
           // a bunch of ignored folders/files
        ],
        env: {
            NODE_ENV: 'development',
        },
        env_production: {
            NODE_ENV: 'production'
        },
        error_file: 'D:/src/nr/logout.log',  // default: $HOME/.pm2/logs/XXXerr.log
        out_file: 'D:/src/nr/logout.log',      // default: $HOME/.pm2/logs/XXXout.log
        pid_file: 'D:/src/nr/nrtest.pid',      // default: $HOME/.pm2/pid/app-pm_id.pid
        combine_logs: true,
        merge_logs: true,
    }],
}

This is how it is run pm2 start ecosystem.config.js && pm2 logs though I use an npm package.json file with defined scripts that do it for me.

As you can tell, auto-running Node-RED on Windows isn't quite as simple as on Linux. You will at least need to do some reading up on PM2.

Way way over my head lol. I did read on the pm2 website, and followed the instructions, the result was not working as seen in my post. I do not have/need the ecosystem config as luckily I only have/need 1 instance of Node Red. I had it running in the past, but unfortunately the SSD crashed, and no backup... (note to self - backup EVERYTHING next time...)
Maybe you have an idea if I tell you my setup.
I installed node.js, then node-red via npm, for global use. Node Red is installed in C:/Users/lenov/AppData/Roaming/npm/
I can start it in a cmd window by typing 'node-red', or double clicking node-red.cmd in above directory

So in my setup, the node.js script that actually runs node-red is here D:/src/nr/node_modules/node-red/red.js so yours is probably at C:/Users/lenov/AppData/Roaming/npm/node_modules/node-red/red.js or something like that, find the script then you can run it manually with node C:/Users/lenov/AppData/Roaming/npm/node_modules/node-red/red.js or wherever it actually is.

Then I think (because I've not used a manual setup of PM2 for years) that you can probably do something like pm2 node C:/Users/lenov/AppData/Roaming/npm/node_modules/node-red/red.js? Please do go read the docs for PM2 though, it is your machine not mine.

Thanks will give it a try (and read) tomorrow

I had to change it slightly to pm2 start C:/Users/lenov/AppData/Roaming/npm/node_modules/node-red/red.js`
that demonized (whatever that is lol) it and started it without errors. Now I have to figure out how to restart NR automatically in pm2 when it crashes. PM2 already autostarts with Windows, so I'm nearly there.
Thanks for your help, much appreciated

After command :

pm2 start C:/Users/lenov/AppData/Roaming/npm/node_modules/node-red/red.js`

don't forget to save your task :

pm2 save --force

pm2 will restart automatically node-red, there is no need to do anything extra

I did pm2 save, but not with --force. Should I do it again ?

You can try, there will be no problem

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.