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

Netlify Dev: Add support for .env files #714

Merged
merged 4 commits into from
Feb 19, 2020
Merged

Conversation

RaeesBhatti
Copy link
Contributor

- Summary
Allows users to use .env files for their site generators and Netlify functions written in JS, when using netlify dev.
Fixes: #457
Fixes: #444
Fixes: #474
Fixes: #405

- Test plan

  • Add .env file to your function directory with SOMETHING=true and see if it is accessible in function using process.env while running through netlify dev
  • Add a similar .env file to the root of your project and see if it is available to your site framework (etc. React etc.) when running through netlify dev

- Description for the changelog

  • Use dotenv package to parse .env file if exists and set the env variables to newly spawned site server process.
  • Do the same for Netlify functions builder when running through netlify dev
  • Use lambda-local to serve JS functions and pass path to function local .env file as parameter when executing

- A picture of a cute animal (not mandatory but encouraged)
🙈

@RaeesBhatti RaeesBhatti requested a review from a team as a code owner February 19, 2020 12:32
@swyxio
Copy link
Contributor

swyxio commented Feb 19, 2020

looks good but needs docs and tests. got to start having a habit of adding these alongside features or it just never gets done

Copy link
Contributor

@fool fool left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I can't speak to the implementation, I think this is what is so desperately desired by our users. Best way to be sure it suits their needs is to let them test it in prod :)

@RaeesBhatti RaeesBhatti merged commit 4f9c7cd into master Feb 19, 2020
@RaeesBhatti RaeesBhatti deleted the raees/dev-env-support branch February 19, 2020 18:32
@unclejustin
Copy link

So this seems to work for new variables in .env, but if I have an existing variable in netlify.com the "remote" variable overrides my local .env. This seems backwards to me. The whole point of a .env file is to use "dev" api key for local development and a production key in actual production. Am I missing something?

@RaeesBhatti
Copy link
Contributor Author

@unclejustin I can confirm this behavior for Netlify Function. The cause for this is that dotenv doesn't override env variables that are already set. Please hold on while we figure this out.

@RaeesBhatti
Copy link
Contributor Author

Being discussed here: #734
cc @unclejustin @jrunestone

@oles
Copy link

oles commented Mar 12, 2020

@RaeesBhatti

No matter what I do - I cannot access any variables from my .env file in my functions.
It doesn't matter if the .env file is in my root folder, inside the functions folder, inside a function folder.

netlify dev do report: Overriding the following env variables with .env file: BISNODE_API_KEY,BISNODE_API_SECRET

Though process.env do not contain neither.

On a side note, this is the third bothersome quirk with netlify dev I've experienced with your functions offering. I'd really like to use your product, and it saddens that I'm having such a hard time with these seemingly basic things

I'm on netlify-cli/2.38.0 linux-x64 node-v10.19.0

@RaeesBhatti
Copy link
Contributor Author

@oles Can you please share an example project with this problem!

@oles
Copy link

oles commented Mar 12, 2020

@RaeesBhatti Sure - I'll get to it tomorrow 👍

@oles
Copy link

oles commented Mar 13, 2020

@RaeesBhatti Here you go: https://github.com/oles/netlify-functions-env-example

Just run netlify dev in it, verify that it reports Overriding the following env variables with .env file: API_KEY,API_SECRET.

Then open http://localhost:8888/.netlify/functions/example, and see that it is blank, and that it logs out two undefineds in the console.

They should be the values from the .env file, as accessed in the function with const {API_KEY, API_SECRET} = process.env

@RaeesBhatti
Copy link
Contributor Author

Thanks for reporting this @oles. This was caused by a silly mistake, we've added tests to make sure that doesn't happen again: #748

@unclejustin
Copy link

🍻 Cheers for the transparency @RaeesBhatti !

@oles
Copy link

oles commented Mar 13, 2020

@RaeesBhatti Perfect! Thank you!

@RaeesBhatti
Copy link
Contributor Author

A fix for this has been released in netlify-cli@2.39.0

@fjeddy
Copy link

fjeddy commented May 4, 2020

This only listens for for variables in .env and ignores alternatives like .env.production and .env.development

@wesbos
Copy link

wesbos commented May 5, 2020

Yeah this is confusing as Gatsby uses the above env files, not just.env

@JLarky
Copy link

JLarky commented May 25, 2020

Yeah this is confusing as Gatsby uses the above env files, not just.env

yes, I was also confused by that so I created #924 hope that helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants