Home > OS >  is there a difference between env / ENV and PORT / port in node js
is there a difference between env / ENV and PORT / port in node js

Time:11-30

I'm new to node.js so recently saw the following code used for the port connection :

process.ENV.PORT

and have also seen someone use process.env.PORT and even process.env.port. now as far as I know JS variables and properties are case sensitive, so how come people are using different versions. My IDE seems to recommend everything in lower case, so I'm curious are all of the above valid or did I get some wrong.

What I wish to know is :

  • whether all of the above are valid.

  • Why are they valid (JS is case sensitive)

  • and is there a difference between any of them

CodePudding user response:

Having process.env.PORT or process.env.port is up to you. You chose to call whatever you want your variable port. And it is process.env in lowercase in Node's official doc. Here is the link if you wanna read it.

CodePudding user response:

Answering your questions:

whether all of the above are valid. no, they are not. process.ENV is not the same as process.env. In fact the built-in process does not have an ENV property; so it is not valid unless you create a process.ENV property (wichi is a very bad idea btw) But in windows process.env.PORT and process.env.port they are interchangeably valid

Why are they valid ? because eventhough JS is case sensitive, the standard process.env property is, under windows, for cross-platform compatibility.

is there a difference between any of them ? .env and .ENV are different totally different .ENV does not even exist; on windows between .port and .PORT there is not any practical difference.

CodePudding user response:

process.env is an object created by Node.js environment which contains some accessible internal keys and can be extended / edited programmatically. It means you can define or edit any properties you want.
So...

whether all of the above are valid
Both of them are valid because any key is valid (even process.env.notReal), which one to use is only your choice.

Why are they valid (JS is case sensitive)
Sure, JS is case sensitive, so process.env.PORT is not the same as process.env.port - they are two different properties. Variable naming policy is up to you.

and is there a difference between any of them
I guess you already got the answer, it's just a code style.

  • Related