简体   繁体   中英

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". .Env file is deployed to Azure with correct entries and the function executes correctly when I hard code the config variables to test. I assume I need to do something differently to get this to work on 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.

It is recommended that defining all your .env content in Azure function application settings: 在此处输入图像描述

Simple demo to get this value:

在此处输入图像描述

Related doc sees here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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