Skip to content
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

Read .env files in the `functions` directory #60

Open
kentcdodds opened this issue Mar 29, 2019 · 5 comments

Comments

Projects
None yet
5 participants
@kentcdodds
Copy link
Member

commented Mar 29, 2019

I configure env variables in the netlify UI, but locally I use .gitignored .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 :)

@philhawksworth

This comment has been minimized.

Copy link
Contributor

commented Apr 1, 2019

Perhaps netlify dev could access and locally use environment variables defined in the Netlify UI, but allow any environment variables defined in a local .env file to override those found remotely.

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.

@sw-yx

This comment has been minimized.

Copy link
Contributor

commented Apr 5, 2019

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 process.env variables will override - but we dont read the .env file for you, you'd have to do it on your own.

if this is satisfactory to @kentcdodds, we can close this, but if anyone feels strongly that we should auto-read a .env file in the root directory, i'm open to the idea but want buyin from everyone. also have to consider conventions like https://github.com/kentcdodds/kentcdodds.com which has .env.production and .env.development

@philhawksworth

This comment has been minimized.

Copy link
Contributor

commented Apr 5, 2019

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 netlify dev but reasonable to have that as next iteration in my view.

@owenconti

This comment has been minimized.

Copy link

commented Apr 10, 2019

I'm finding that I cannot override any process.env variables when using dotenv in combination with netlify dev.

I need to check process.env.NODE_ENV to determine which callback URL to provide when authenticating users via Twitter:

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 dotenv is called (via the log):

Request from ::ffff:127.0.0.1: GET /.netlify/functions/auth/twitter
isProduction false
Loading dotenv config...

When using netlify-lambda serve this works fine, but with netlify dev (after running netlify link) all the environment variables are the "production" ones, not my local .env file ones.

@iamskok

This comment has been minimized.

Copy link

commented Jun 10, 2019

@owenconti were you able to figure it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.