Using dotenv to load environmental variables on a project specific basis

This post is a follow up to this initial feature request.

I would post a response but the thread has been closed to replies.

Anyway, I have been having a play with loading environmental variables from file on a project specific basis. It seems like a relatively easy task to accomplish, locally anyway.

I had a look through the node-RED source code and in:

node_modules\@node-red\runtime\lib\storage\localfilesystem\projects\Project.js

Found the method that loads the project during initialization and when a project is switched.

To allow for environmental variables to be loaded on a project specific basis I simply added this line of code to the resolution of all promises:

return Promise.all(promises).then(function(results) {
	require("dotenv").config({ path: `${project.path}/.env`, override: true });
    return project;
 })

It seems to work a treat :slight_smile:

There is a prerequisite for dotenv to be included as a dependency and it could be refined to the extent that the override flag true/false is included as a project related property in the main settings file.

Anyway it could be a useful feature...

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.