I'm new to the Node.JS. I found few articles says we can use .env file to setup the process.env variable, e.g.,
我是Node.JS的新手。我发现很少有文章说我们可以使用.env文件来设置process.env变量,例如,
PORT = 8081
but when I run the program in my node, it is still 8080 PORT (by default). The question is how can I setup the env variable in Node without any other 3rd party module's help? (I found there are few 3rd party package to management the env config, but... it is kind confused, different package might have different rule and more complex use cases; I want to start from clear way to study purely nodejs)
但是当我在我的节点中运行程序时,它仍然是8080 PORT(默认情况下)。问题是如何在Node中设置env变量而不需要任何其他第三方模块的帮助? (我发现很少有第三方软件包来管理env配置,但是......它很混乱,不同的软件包可能有不同的规则和更复杂的用例;我想从明确的方式开始纯粹研究nodejs)
Update
I have read Node Environment Setting post on *, but they are refer using 3rd party package, none of them tells the detail steps. (Either windows system environment, or Linux environment variables... but how can I place the setting into my project folder?!)
我已经在*上阅读了Node Environment Setting帖子,但它们是使用第三方软件包引用的,它们都没有说明详细步骤。 (无论是Windows系统环境,还是Linux环境变量......但是如何将设置放入我的项目文件夹?!)
2 个解决方案
#1
5
Dotenv file have become the most popular mode to separate configuratione from app, using system environment variables (see 12factor config).
Dotenv文件已经成为使用系统环境变量将配置与app分开的最流行模式(参见12factor config)。
On node there exists a lot of libraries for loading config from .env file. The most popular is motdotla/dotenv. You can read a lot of examples on readme file about the usage of this library
在节点上有很多用于从.env文件加载配置的库。最受欢迎的是motdotla / dotenv。您可以在自述文件中阅读有关此库用法的大量示例
#2
1
Make a config.js file with the following content:
使用以下内容创建config.js文件:
module.exports = {
bar: 'someValue',
foo: 'otherValue'
...
}
Then you can do this in some file:
然后你可以在一些文件中执行此操作:
const config = require('./config');
let foo = config.foo;
#1
5
Dotenv file have become the most popular mode to separate configuratione from app, using system environment variables (see 12factor config).
Dotenv文件已经成为使用系统环境变量将配置与app分开的最流行模式(参见12factor config)。
On node there exists a lot of libraries for loading config from .env file. The most popular is motdotla/dotenv. You can read a lot of examples on readme file about the usage of this library
在节点上有很多用于从.env文件加载配置的库。最受欢迎的是motdotla / dotenv。您可以在自述文件中阅读有关此库用法的大量示例
#2
1
Make a config.js file with the following content:
使用以下内容创建config.js文件:
module.exports = {
bar: 'someValue',
foo: 'otherValue'
...
}
Then you can do this in some file:
然后你可以在一些文件中执行此操作:
const config = require('./config');
let foo = config.foo;