Dark Mode
Image

Node.js "process.env" Property

The "process.env" functionality is an inbuilt application in the nodejs to display the user environment. It is the programming interface of the nodejs process module to get the user environment as an output.

Syntax

The following syntax uses in the output function to get and display data.

  1. process.env  
  2. OR  
  3. console.log(process.env);  

Return Value: This property shows an object of the user environment.

Examples

Below examples display the operation and the use of the nodejs process.env property.

Example1

  1. console.log("Node.js process.env Property");  
  2. // Node.js program to operate the process.env Property  
  3. // Use process module of the server  
  4. const process_data = require('process');  
  5. //show the process.env property information  
  6. console.log(process.env);  

Output

The image shows the "process.env" property information.

Node.js process.env Property

Example 2

The example shows the basic data of the server using the property. We can get server-related information using the process.

  1. // Node.js program to operate the process.env Property  
  2. console.log("Node.js process.env Property");  
  3. //It contains the processing module  
  4. const process = require('process');  
  5. // Printing process.env property value  
  6. var no_env = 0;  
  7. // Calling process.env with the variable  
  8. var env_var = process.env;  
  9. // Iterating through all returned data  
  10. for (var key in env_var) {  
  11. // Print value  
  12. console.log(key + " :\t\t\t " + env_var[key]);  
  13. no_env++;  
  14. }  
  15. // display count  
  16. console.log("total no of values available = "  
  17. + no_env);  
  18. // Access one by one all the information  
  19. console.log("operating system: " + env_var['OS']);  
  20. console.log("alluserprofile: " + env_var['ALLUSERSPROFILE']);  
  21. console.log("public directory: " + env_var['PUBLIC']);  

Output

The image shows the "process.env" property information.

Node.js process.env Property

Example 3

The example shows the basic data of the server using the property. Here, we can add and delete new data with the process.env property.

  1. // Node.js program to operate the process.env Property  
  2. console.log("Node.js process.env Property");  
  3. // it contains the process module  
  4. const process = require('process');  
  5.  // Printing process.env property value  
  6. var env_proc = process.env;  
  7. console.log("operating system: " + env_proc.OS);  
  8. console.log("all use r  profile: " + env_proc.ALLUSERSPROFILE);  
  9. console.log("public directory: " + env_proc.PUBLIC);  
  10.  // Setting new data  
  11. env_proc.getd= "gekcho custom data";  
  12. console.log("stored in env.gekcho: " + env_proc.getd);  
  13.   // Delete data  
  14. delete env_proc.getd  
  15. console.log("contain in env.gekcho: " + env_proc.getd);  

Output

The image shows the "process.env" property information.

Node.js process.env Property

Example 4

The example shows the basic data of the server using the property. We can get the path and port of the process.

  1. // Node.js program to operate the process.env Property  
  2. console.log("Node.js process.env Property");  
  3. //basic process property  
  4. console.log(process.env);   
  5. //get the path of the process  
  6. const PATH_process = process.env.PATH   
  7. // Calling process.env for the port  
  8. const PORT_process = parseInt(process.env.PORT);   
  9. // Access one by one all the information  
  10. console.log("Process Path: " + PATH_process);  
  11. console.log("Process PORT: " + PORT_process);  

Output

The image shows the "process.env" property information.

Node.js process.env Property

Conclusion

The nodejs process.env is the environmental variable to get system information. The process detail displays a single keyword or line in the console tab. The method is essential and easy to use for the developer and users.

Comment / Reply From