Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
};
like image 390
Graham Avatar asked Oct 22 '25 03:10

Graham


1 Answers

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: enter image description here

Simple demo to get this value:

enter image description here

Related doc sees here.

like image 62
Stanley Gong Avatar answered Oct 23 '25 18:10

Stanley Gong