/** * Load environment variables from '.env' file */ loadEnvFile() { if (this.flags.env || this.flags.envfile) { try { const dotenv = require("dotenv"); if (this.flags.envfile) dotenv.config({ path: this.flags.envfile }); else dotenv.config(); } catch(err) { throw new Error("The 'dotenv' package is missing! Please install it with 'npm install dotenv --save' command."); } } }
jwt.verify(token, dotenv.parsed.SECRET_KEY, function(err, decoded) { console.log('decoded ? ', decoded); if (decoded) { next(); } else { console.log("login first!"); return next({ status: 401, message: 'Please log in first!' }); } });
dotenv() { const env = dotenv.config() return env }
// Load /path/to/project-root/.env file and returns the key-value object // Load .env.staging, .env.production if NODE_ENV environment variable is defined. function main() { const projectRoot = getProjectRoot() const nodeEnv = getNodeEnv() const file = loadEnvFile(projectRoot, nodeEnv) return dotenv.parse(file) }
dotenvFiles.forEach((dotenvFile) => { if (fs.existsSync(dotenvFile)) { dotenvExpand(dotenv.config({ path: dotenvFile })); } });
/* * Initializes and returns an instance of the serverless sdk * @param ${string} orgName - the serverless org name. Must correspond to the access key in the env */ const getServerlessSdk = (orgName) => { const sdk = new ServerlessSDK({ accessKey: process.env.SERVERLESS_ACCESS_KEY || dotenv.SERVERLESS_ACCESS_KEY, context: { orgName, }, }); return sdk; }
/* * Fetches AWS credentials from the current environment * either from env vars, or .env file in the /tests directory */ const getCredentials = () => { const credentials = { aws: { accessKeyId: process.env.AWS_ACCESS_KEY_ID || dotenv.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || dotenv.AWS_SECRET_ACCESS_KEY, }, }; if (!credentials.aws.accessKeyId || !credentials.aws.accessKeyId) { throw new Error('Unable to run tests. AWS credentials not found in the envionrment'); } return credentials; }
configPaths.filter(fs.existsSync).forEach(path => { console.log('Loading env file:', path); dotenv.config({ path }); });
public loadConfig(): void { // 避免重复加载 if (this.cache) { return; } // 如果是生产环境,一些所需的信息会通过CI工具注入到process.env中 if (this.mode === 'production') { this.cache = process.env; return; } // 本地开发环境,需要读取项目目录中的 .env 文件 const filepath = path.join(__dirname, '..', '.env'); if (!fs.existsSync(filepath)) { throw new Error(`Please create configuration file: ${filepath}`); } const localEnv = dotenv.config({ path: filepath }).parsed; this.cache = { ...process.env, ...localEnv, }; }
public loadConfig(): void { // 避免重复加载 if (this.cache) { return; } // 如果是生产环境,一些所需的信息会通过CI工具注入到process.env中 if (this.mode === 'production') { this.cache = process.env; return; } // 本地开发环境,需要读取项目目录中的 .env 文件 const filepath = path.join(__dirname, '..', '.env'); if (!fs.existsSync(filepath)) { throw new Error(`Please create configuration file: ${filepath}`); } const localEnv = dotenv.config({ path: filepath }).parsed; this.cache = { ...process.env, ...localEnv, }; }