[英]npm dotenv environment variables not recognized in nodejs module
Here is my project structure: 这是我的项目结构:
package.json : package.json :
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon $NODE_DEBUG_OPTION server/boot.js --exec babel-node",
"start": "nodemon server/boot.js --exec babel-node",
"build": "babel server -d dist/server",
"serve": "node dist/server/boot.js"
},
The main file is server/boot.js : 主文件是server / boot.js :
import dotenv from 'dotenv';
import path from 'path';
dotenv.load({path: path.join(__dirname, '.env')});
import _ from 'underscore';
import configs from './config/index';
The server/config/index.js is only a barrel file that imports the other config files: server / config / index.js只是一个导入其他配置文件的桶文件:
import app from './app';
import database from './database';
export default Object.assign({}, app, database);
In each of the config files I am not able to access any properties of the process.env object that are defined in the .env file. 在每个配置文件中,我都无法访问.env文件中定义的process.env对象的任何属性。
Here is one of the config files for reference: 这是供参考的配置文件之一:
export default {
app: {
host: process.env.HOST || 'localhost',
port: process.env.PORT || 9000,
}
}
Here process.env.HOST is undefined, but the key is present in the .env file. 这里process.env.HOST是未定义的,但是密钥存在于.env文件中。
What I am doing wrong? 我做错了什么?
process.env object that are defined in the .env file.
.env文件中定义的process.env对象。
Can you please be more specific about the process.env file? 您能否更详细地了解process.env文件?
As per https://www.npmjs.com/package/dotenv#rules the file should be in the format: 根据https://www.npmjs.com/package/dotenv#rules ,文件应采用以下格式:
VAR1=value1
VAR2=value2
and not in 而不是
export default {
VAR1: 'value1',
VAR2: 'value2'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.