I would like to use different credentials in my functions depending on my NODE_ENV variable. I have a Gatsby project and when I do netlify dev Gatsby sets NODE_ENV === development and gatsby develop command sets it to production (at least this is what I get when I log NODE_ENV from gatsby-config.js). However it doesn’t seem to apply to my functions.
Please have a look at Auth.js file
Update:
Some interesting observations (LMK if they are not related to the current issue and have to be posted in another place). I was following this post about .env variables in Gatsby project.
It suggests introducing another variable, e.g. GATSBY_ACTIVE_ENV and set it up manually. I was able to run GATSBY_ACTIVE_ENV=development netlify dev as expected, though adding GATSBY_ACTIVE_ENV=development netlify dev command under scripts in package.json and running yarn develop was causing an endless loop. After getting in the loop once you are not able to run GATSBY_ACTIVE_ENV=development netlify dev anymore without getting in the loop.
Thanks for your reply, but I’m running in a pretty weird issue with process.env.CONTEXT.
When I run CONTEXT=development netlify dev netlify starts an endless loop and Lambda server keeps switching ports. Not exactly sure what can cause it:
✘ ✝ ~/dev/gatsby-dev-blog-starter master± yarn develop
yarn run v1.12.3
$ CONTEXT=development netlify dev
◈ Netlify Dev ◈
◈ Injected build setting env var: GATSBY_DEV_GITHUB_CLIENT_ID
◈ Injected build setting env var: GATSBY_DEV_GITHUB_CLIENT_SECRET
◈ Injected build setting env var: GATSBY_DEV_HOST_URL
◈ Injected build setting env var: GATSBY_FUNCTION_PASSWORD
◈ Injected build setting env var: GATSBY_GITHUB_CLIENT_ID
◈ Injected build setting env var: GATSBY_GITHUB_CLIENT_SECRET
◈ Injected build setting env var: GATSBY_HOST_URL
◈ Injected build setting env var: GATSBY_DEV_FUNCTION_PASSWORD
◈ Starting Netlify Dev with gatsby
Waiting for localhost:8000.
◈ Lambda server is listening on 34567
$ npm run develop
npm WARN lifecycle The node binary used for scripts is /var/folders/s7/vtbv89nd3f99nlx5y9h94qk00000gn/T/yarn--1560352104668-0.36838903564793/node but npm is using /Users/skok/.nvm/versions/node/v11.5.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
> gatsby-dev-blog-starter@0.1.0 develop /Users/skok/dev/gatsby-dev-blog-starter
> CONTEXT=development netlify dev
.◈ Netlify Dev ◈
.◈ Starting Netlify Dev with gatsby
Waiting for localhost:8000.
◈ Lambda server is listening on 55396
$ npm run develop
.npm WARN lifecycle The node binary used for scripts is /var/folders/s7/vtbv89nd3f99nlx5y9h94qk00000gn/T/yarn--1560352107238-0.9451571250276476/node but npm is using /Users/skok/.nvm/versions/node/v11.5.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
> gatsby-dev-blog-starter@0.1.0 develop /Users/skok/dev/gatsby-dev-blog-starter
> CONTEXT=development netlify dev
..◈ Netlify Dev ◈
.◈ Starting Netlify Dev with gatsby
.Waiting for localhost:8000.
◈ Lambda server is listening on 55412
$ npm run develop
.npm WARN lifecycle The node binary used for scripts is /var/folders/s7/vtbv89nd3f99nlx5y9h94qk00000gn/T/yarn--1560352109773-0.46483751576396815/node but npm is using /Users/skok/.nvm/versions/node/v11.5.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
> gatsby-dev-blog-starter@0.1.0 develop /Users/skok/dev/gatsby-dev-blog-starter
> CONTEXT=development netlify dev
...◈ Netlify Dev ◈
.. ◈ Starting Netlify Dev with gatsby
.Waiting for localhost:8000.
◈ Lambda server is listening on 55436
$ npm run develop
..npm WARN lifecycle The node binary used for scripts is /var/folders/s7/vtbv89nd3f99nlx5y9h94qk00000gn/T/yarn--1560352112263-0.5329314860894214/node but npm is using /Users/skok/.nvm/versions/node/v11.5.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
> gatsby-dev-blog-starter@0.1.0 develop /Users/skok/dev/gatsby-dev-blog-starter
> CONTEXT=development netlify dev
You probably wouldn’t want to set the CONTEXT env var yourself as it’s a default var as mentioned in the continuous deployment docs:
CONTEXT : Name of the context a deploy is built around, it can be production , deploy-preview or branch-deploy .
What David meant was to use that pre-existing variable as a way to know when your deploy is production or now. Let me know if that helps clarify things a bit.
Ok, there are a lot of moving parts in things you have been trying and we need to take them one at a time.
Infinite Loop (netlify dev)
Do not use netlify dev in your package.json script under the development command.
Best to use it only as a terminal command, which will start the script itself if it exists from what I am seeing.
If you want to check CONTEXT locally, it might be best to setup process.env.CONTEXT prior to running the netlify dev command.
This will need to be set outside running netlify dev so you should do that using something like cross-env or equivalent setup.
In example yarn run cross-env CONTEXT=development netlify dev
Note: Something to remember here is that during the build, these values need to be baked into the build, because there is not a valid process.env.CONTEXT when the function is deployed to the aws lambda servers. This is accomplished in the netlify-lambda build command.
@iamstock I made a PR to fix the infinite loop and builds. This was something I wanted to see how it was setup, so it helped me to see the easiest way to add functions to a Gatsby build and local development workflow using netlify dev
You will have to fix the local development endpoints, because they are not going to be at <localhost>/.netlify/functions/<function>, but at <localhost>/<function>