# Port number of running node-red instance

**URL:** <https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166>\
**Category:** General\
**Created:** [10 January 2020 16:28 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166 "2020-01-10T16:28:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meng](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Meng](https://discourse.nodered.org/u/Meng)\
**Post date:** [10 January 2020 16:28 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/1 "2020-01-10T16:28:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 January 2020 16:38 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/2 "2020-01-10T16:38:18Z")

</div>

Hi @Meng

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 January 2020 16:46 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/3 "2020-01-10T16:46:06Z")

</div>

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/`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 January 2020 17:40 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/4 "2020-01-10T17:40:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Meng](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Meng](https://discourse.nodered.org/u/Meng)\
**Post date:** [13 January 2020 07:45 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/5 "2020-01-13T07:45:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 January 2020 09:16 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/6 "2020-01-13T09:16:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Meng](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Meng](https://discourse.nodered.org/u/Meng)\
**Post date:** [13 January 2020 12:58 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/7 "2020-01-13T12:58:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 January 2020 13:29 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/8 "2020-01-13T13:29:15Z")

</div>

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:

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

```

You could do:

```auto
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.

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [23 April 2020 15:24 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/9 "2020-04-23T15:24:17Z")

</div>

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  
}
```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 April 2020 22:07 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/10 "2020-04-23T22:07:54Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [24 April 2020 05:37 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/11 "2020-04-24T05:37:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 April 2020 06:33 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/12 "2020-04-24T06:33:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [25 April 2020 03:32 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/13 "2020-04-25T03:32:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zainisma](https://avatars.discourse-cdn.com/v4/letter/z/aeb1de/32.png) [@zainisma](https://discourse.nodered.org/u/zainisma)\
**Post date:** [11 May 2020 23:51 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/14 "2020-05-11T23:51:42Z")

</div>

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...

```auto
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: -

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [12 May 2020 00:19 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/15 "2020-05-12T00:19:35Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![zainisma](https://avatars.discourse-cdn.com/v4/letter/z/aeb1de/32.png) [@zainisma](https://discourse.nodered.org/u/zainisma)\
**Post date:** [12 May 2020 00:26 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/16 "2020-05-12T00:26:52Z")

</div>

Thanks for clearing that up ristomatti.

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

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [12 May 2020 00:34 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/17 "2020-05-12T00:34:08Z")

</div>

> [@zainisma](#):
>
> 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](https://stackoverflow.com/questions/43024906/pass-node-js-environment-variable-with-windows-powershell)

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

---

<div class="post-metadata">

**Author:** ![zainisma](https://avatars.discourse-cdn.com/v4/letter/z/aeb1de/32.png) [@zainisma](https://discourse.nodered.org/u/zainisma)\
**Post date:** [12 May 2020 00:40 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/18 "2020-05-12T00:40:49Z")

</div>

> [@ristomatti](#):
>
> power shell environment variable node

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

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [12 May 2020 00:43 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/19 "2020-05-12T00:43:20Z")

</div>

> [@zainisma](#):
>
> 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. 🙂

---

<div class="post-metadata">

**Author:** ![RobC](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@RobC](https://discourse.nodered.org/u/RobC)\
**Post date:** [5 June 2023 03:46 UTC](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166/20 "2023-06-05T03:46:15Z")

</div>

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

[Next page](https://discourse.nodered.org/t/port-number-of-running-node-red-instance/20166.md?page=2)
