简体   繁体   English

如何在 Azure function 中的 Nodejs 中读取 .env 文件变量

[英]How to read .env file variables in Nodejs within Azure function

I have the following code which works fine locally, but when I deploy to an Azure function it fails to read in the contents of the.env file at runtime, when debugging each of the config items is "undefined".我有以下代码在本地运行良好,但是当我部署到 Azure function 时,它无法在运行时读取 .env 文件的内容,当调试每个配置项时“未定义”。 .Env file is deployed to Azure with correct entries and the function executes correctly when I hard code the config variables to test. .Env 文件以正确的条目部署到 Azure 并且当我硬编码配置变量以进行测试时,function 正确执行。 I assume I need to do something differently to get this to work on Azure?我想我需要做一些不同的事情才能让它在 Azure 上工作?

const sql = require('mssql')
require('dotenv').config();

const dbConfig = {
    server: process.env.databaseServer,
    database: process.env.databaseName,
    user: process.env.databaseUser,
    password: process.env.databasePassword,
    port: 1433,
    options: {
        encrypt: true,
        "enableArithAbort": true
    }
};

Azure function is a packed service, its process.env has reloaded properties of the Azure function environment, by default, it will not load your .env file. Azure function is a packed service, its process.env has reloaded properties of the Azure function environment, by default, it will not load your .env file.

It is recommended that defining all your .env content in Azure function application settings:建议在 Azure function 应用程序设置中定义所有.env内容: 在此处输入图像描述

Simple demo to get this value:获取此值的简单演示:

在此处输入图像描述

Related doc sees here .相关文档在这里看到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM