How to install process.env from a file in NodeJS?

I am new to Node.JS. I found several articles about how we can use the .env file to configure the process.env variable, for example,

PORT = 8081 

but when I run the program in my node, it is still 8080 PORT (by default). The question is how to configure the env variable in Node without the help of other third-party modules? (I found that there are several third-party packages for env configuration management, but ... it is confusing, another package may have different rules and more complex use cases, I want to start with a clear way to learn pure nodejs)

Update

I read a post about setting up a Node environment in StackOverFlow, but they refer to a third-party package, none of them provide detailed steps. (Either the Windows system environment or the Linux environment variables ... but how can I put this parameter in the project folder ?!)

+5
source share
2 answers

The Dotenv file has become the most popular mode for sharing configuration from an application using system environment variables (see 12factor config ).

There are many libraries in node for loading config from an .env file. The most popular is motdotla / dotenv . You can read many examples in the readme file about using this library.

+4
source

Create a config.js file with the following contents:

 module.exports = { bar: 'someValue', foo: 'otherValue' ... } 

Then you can do it in some kind of file:

 const config = require('./config'); let foo = config.foo; 
+2
source

Source: https://habr.com/ru/post/1264554/


All Articles