How to change NODE_ENV for netlify functions

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.

Yo yo!

You should be able to switch based on the env vars listed here: Build configuration overview | Netlify Docs

process.env.CONTEXT should* work for ya inside the function

2 Likes

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

Here is the link on netlify functions in my repo - https://github.com/iamskok/gatsby-dev-blog-starter/tree/master/.netlify/functions

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.

I’m not setting CONTEXT var just trying to retrieve the value of process.env.CONTEXT, which is undefinied. My code looks like:

if (process.env.CONTEXT === 'production') {
  host = process.env.GATSBY_HOST_URL
  clientId = process.env.GATSBY_GITHUB_CLIENT_ID
  password = process.env.GATSBY_FUNCTION_PASSWORD
} else if (process.env.CONTEXT === 'development') {
  host = process.env.GATSBY_DEV_HOST_URL
  clientId = process.env.GATSBY_DEV_GITHUB_CLIENT_ID
  password = process.env.GATSBY_DEV_FUNCTION_PASSWORD
}

When I run netlify dev I expect process.env.CONTEXT to equal to development and in the rest of the cases I expect it to be production.

netlify.toml:

[settings]
  ID = "fbe445b2-665a-4b6b-befb-98c0249e523e"
[build]
  base = "/"
  publish = "public"
  command = "yarn build"
  functions = ".netlify/functions"
[build.environment]
  NODE_VERSION = "11.5.0"
  YARN_VERSION = "1.3.2"
  YARN_FLAGS = "--no-ignore-optional"
[context.production]
  NODE_ENV = "production"
[context.deploy-preview]
  NODE_ENV = "production"
[context.branch-deploy] 
  NODE_ENV = "production"

The rest of the code can be found in the repo

This question has also been asked on StackOverflow

Updated the question with the new information

Hi @iamskok, just to be clear, are your issues just when using netlify dev or are you having problems in production on Netlify as well?

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.

Best to try something like:

  "scripts": {
    "build": "npm run build:functions && npm run build:gatsby",
    "build:gatsby": "gatsby build",
    "build:functions": "netlify-lambda build src/functions",
    "develop:functions": "netlify-lambda serve src/functions",
    "develop": "gatsby develop",
    ....
  },

For development on your local machine, run netlify dev and it will start the lambda server and run the develop script (gatsby develop).

Netlify Functions location

netlify-lambda build src/functions will build your function bundles into /functions, so in the netlify.toml:

[build]
  base = "/"
  publish = "public"
  command = "yarn build"
  functions = "functions"

Note: best to not include the /functions folder into your repository, since it will be part of your build and get created on success.

Being able to check process.env.CONTEXT locally

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>