-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DX] Read .env files in the functions
directory
#474
Comments
Perhaps And if this could happen automatically by using dotenv or similar internally without requiring the developer to manually include that, it would feel like a nice simplification to me. |
sooo.. i just found out that netlify dev ALREADY does this 🤯 https://github.com/netlify/netlify-dev-plugin/blob/master/src/commands/dev/index.js#L196 this is crazy... NO MORE LOCAL .env VARIABLES!!!!!!!!!!!! JUST SET IT IN THE UI AND YOU'RE DONE and to be clear, any set if this is satisfactory to @kentcdodds, we can close this, but if anyone feels strongly that we should auto-read a |
This is a pretty huge first step! Handling the ability to do local overrides automatically would be a very useful utility and a benefit to using |
I'm finding that I cannot override any I need to check const isProduction = process.env.NODE_ENV === 'production';
console.log('isProduction', isProduction);
if (!isProduction) {
console.log('Loading dotenv config...');
require('dotenv').config();
}
// later in the file...
new TwitterStrategy({
callbackURL: process.env.SERVER_URL + '/auth/twitter/callback'
}, And I can confirm that
When using |
@owenconti were you able to figure it out? |
Although this is indeed an interesting feature, I usually don't want (most of) the production or staging env variables in my local dev system. This is kinda the point of env variables: they're different between environments 😅 Automatically reading |
We are going to try to get around this for now by making a copy of the site in app.netlify.com which will basically act as a repo for local dev environment variables. We will link to that from the ntl cli so it will pull down those env vars. I would much rather just be able to drop a gitignored env in the root of the site that overrides one or two environment variables selectively then uses the injected site values for the rest. |
functions
directoryfunctions
directory
Not sure if this is the right thread for this but I also ran into env variable issues when trying out netlify dev. I would actually wish that .toml file environmental variables be used in netlify dev and made available not just to the site but also to the functions. This would provide a single place to handle environmental variables for both dev and production |
Is there any workaround to get |
Shouldn't Netlify Dev environment just be its own deploy context? Also am personally finding that Netlify kind of clashes with my dotenv setup, so would definitely appreciate a dotenv friendly approach. |
@owenconti This happens because @BrunoQuaresma Here's a snippet which enables overwriting env vars defined in Netlify with env vars from const dotenv = require("dotenv")
const envConfig = dotenv.config();
// "parsed" will be undefined when no .env file was present,
// in which case we need to skip the call to Object.entries
if (envConfig.parsed) {
Object.entries(envConfig.parsed).forEach(([key, value]) => {
process.env[key] = value;
});
} Or as a short versionconst envConfig = require("dotenv").config();
Object.entries(envConfig.parsed || {}).forEach(
([key, value]) => (process.env[key] = value)
);
I too would appreciate out-of-the-box support for
The concept of Some ideas/ramblingsWhat might be nice to have (from my limited understanding) is a way to provide env var overwrites for different deploy contexts through the Netlify UI. That would enable using different API keys for production and preview branches easily. Additionally |
From a security perspective, the reading of .env files would be a really nice addition. In particular, for our use case, we're using Storyblok as a headless cms. In order to interact with their content management api, there is a per-user api key required. It would be ideal for each dev to use their own key during development and have our infra/ops account owner on Storyblok generate the key that is used in production. I'm thinking there would be two parts to this:
Also, to touch on @dferber90's "ramblings" section, my only concern about having |
Env support is coming soon with #616 and new https://github.com/netlify/build pipeline You will be able to reference environment variables in config files key: ${env:MY_ENV_VAR} |
An alternative technique that I've been using recently is setting my setClientSecret(
process.env.NETLIFY_DEV
? process.env.CLIENT_SECRET_DEV
: process.env.CLIENT_SECRET_PROD
); I don't know how well-documented this |
I configure env variables in the netlify UI, but locally I use
.gitignore
d.env
files and https://www.npmjs.com/package/dotenv-cli to run netlify-lambda with the right env variables.It'd be cool if netlify-dev-plugin could solve this problem somehow :)
The text was updated successfully, but these errors were encountered: