Port number of running node-red instance

How can i get the portnumber of the current node-red instance that is running. I can not get port: process.env.PORT || 1880 working.

Hi @Meng

Where do you want to get it from? In your flow, in a custom node, somewhere else?

If you just need to know it in order to diagnose a problem then start node-red in a terminal and the startup log will tell you. You will see something like
10 Jan 16:27:36 - [info] Server now running at http://127.0.0.1:1880/

If you need it in your Node-RED flow, you can simply add it to the global variables in settings.js, I do this with the PID so that I can kill Node-RED in a flow if I want to.

I need the information in a flow in order to get the node-red version for a status page. I added

port:process.env.PORT || 1880

to functionGlobal context, but i always get 1880 as a result, even if i am running node-red on another port 1881 e.g.

For that to work, you need to use the PORT env variable to set the port before starting Node-RED.

If you set the port another way, make sure you have the number in a variable and pass that variable to the globals.

What do you mean by use the PORT env variable to set the port before starting? How can i pass the port number to global variables?
I start node red the following in the cmd:

node-red -p 1885 -u . -settings.js flows.json

For the setting.js, I added some information under

functionGlobalContext: {
// os:require('os'),
process:process,
port: process.env.PORT || 1880,

And in the flow I add the follwoing information:

port= global.get('port');
msg.payload=port;

Which gives always 1880 as a result.

To set the port you have three options:

  1. do nothing and Node-RED will use port 1880 by default
  2. set the PORT env var and Node-RED will use that instead
  3. use the -p option and it will use that value and not the value of the PORT env var.

The only place to add something to global context is in the settings file. The settings file does not know if you have used the -p option or not - nor what value you provided.

The only way for the settings file to know what port you are using is if you use the PORT env var.

So instead of using:

node-red -p 1885 -u . -settings.js flows.json

You could do:

PORT=1885 node-red -u . -settings.js flows.json

as that will set the PORT env var for the node-red process.

You can then use process.env.PORT in your settings file to add its value to global context.

1 Like

You mean to say that the below settings is expected to work?.
Unfortunately it is not working for me ,anything wrong ?
functionGlobalContext: {

	 userPort:process.env.PORT  
}
module.exports = {
    // the tcp port that the Node-RED web server is listening on
    uiPort: process.env.PORT || 1880,

I was trying to get port number which I pass as command line using global variable.
I declared a variable userPort and tried to initialize .
Is there any other way to get the global variable in my custom node?

In a node you should be able to access RED.settings.port.

Thanks Nick ,I think you meant RED.settings.uiPort .This worked.

Hello Nick,

Sorry I don't quit get this explanation. When I copy paste the sample command line above...

PORT=1885 node-red -u . -settings.js flows.json

I get this error when I run that command in my Windows10 PowerShell environment...

PS C:\Users\login_username\.node-red> PORT=1885 node-red -u . -settings.js flows.json
PORT=1885 : The term 'PORT=1885' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ PORT=1885 node-red -u . -settings.js flows.json
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (PORT=1885:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Am I supposed to have set PORT = 1885 within my settings.js file?

If YES, where do I do that in the settings.js file?

My settings within settings.js for functionGlobalContext are as follows: -

functionGlobalContext: {
		moment:require('moment'), //Added moment.js functionallity into function block by installing "moment" library into node_modules directory
        moment_timezone:require('moment-timezone'),
		process: process,
		port: process.env.PORT || 1880
		
		// os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

and

module.exports = {
    // the tcp port that the Node-RED web server is listening on
    uiPort: process.env.PORT || 1880,

Appreciate any help to clarify.

Thank you

It looks like you're using Power shell on Windows? Based on a quick web search you'll need to use instead:

$env:PORT="8000" ; node-red -u . -settings.js flows.json
1 Like

Thanks for clearing that up ristomatti.

Would you mind sharing the key words used to search for this solution in Google

Sure, I used the keywords: power shell environment variable node
The answer was based on the first result: javascript - Pass Node.js environment variable with Windows PowerShell - Stack Overflow

Note that I primarily use Linux for everything so I have not tried this.

Thanks again ristomatti

The key words I used was as follows...

set node red global environment in windows 10 command line cmd Powershell

which didn't give me the results I needed. Thanks again

You're welcome. Sometimes "less is more" when it comes to keywords. :slightly_smiling_face:

1 Like

Hi Nick,
Has the settings object been removed from 'RED', this doesn't work for me v3.0.2
Cheers, Rob