question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Env variables appear to be being cached

See original GitHub issue

I’ve posted this both in Stack Overflow as well as in the ‘issues’ of ‘dotenv’ and have not yet solved it. I don’t know that this is a KeystoneJS issue, but it’s the last place I know to look. In short: could KeystoneJS be caching my env variables somewhere?

I am working on a KeystoneJS project, currently running in development mode on NodeJS 12.14.0. About a week or ten days ago I noticed that even though I changed some environment variables in .env the changes were not reflected at run-time. I’ve had to actually introduce new variables for the changes to take effect.

I’ve done searches (using WebStorm and VSC) in my code for the old information, but it only appears in my debug logs. That is to say, if the old value of my env var ANIMAL was ‘rooster’ and I’ve changed it to ‘lizard’, ‘rooster’ does not appear anywhere in my code. And yet ANIMAL keeps having the value rooster at runtime. So that I have to introduce a new variable, NEW_ANIMAL=lizard to make any changes work.

This does not happen with all env variables – for instance, I just added a new one, TEST=1, ran the app, stopped, changed it to TEST=2, and the change worked fine. I do not see a pattern in what variables are affected.

I’m launching the project through Keystone’s launch script, and I’m using TypeScript. My launch script is

"dev": "tsc && cross-env NODE_ENV=development DISABLE_LOGGING=true keystone --entry=tsout/index.js dev --port=4545"

dotenvis being loaded at the top of the entry point to the project, /index.ts,

require('dotenv').config();

which is run each time the project is restarted (for the record, I’m using Babel to be able to use import instead of require throughout the project, but I can’t imagine that matters).

I just spent half an hour with another dev here going over this and neither of us could figure it out (actually we were trying to track down a weird bug and it turned out it was because even though the .env variable had been changed, the app was still reading the old value).

If anyone has ever encountered anything like this before – or has any clues on how to figure it out – I’d be very grateful.

I include my package.json below. I’m running MacOS 10.14.x and NodeJS 12.14.0, for what it’s worth.


    {
      "name": "@keystonejs/example-projects-blank",
      "description": "A blank KeystoneJS starter project.",
      "private": true,
      "version": "5.0.0",
      "author": "The KeystoneJS Development Team",
      "repository": "https://github.com/keystonejs/keystone.git",
      "homepage": "https://github.com/keystonejs/keystone",
      "license": "MIT",
      "engines": {
        "node": ">=10.0.0"
      },
      "scripts": {
        "dev": "tsc && cross-env NODE_ENV=development DISABLE_LOGGING=true  keystone --entry=tsout/index.js dev --port=4545",
        "build": "cross-env NODE_ENV=production keystone build",
        "start": "cross-env NODE_ENV=production keystone start",
        "keystone": "keystone upgrade-relationships --index=tsout/index.js"
      },
      "dependencies": {
        "@apollo/react-hooks": "^3.1.5",
        "@arch-ui/fields": "^3.0.1",
        "@arch-ui/layout": "^0.2.13",
        "@arch-ui/typography": "^0.0.17",
        "@emotion/core": "^10.0.28",
        "@emotion/styled": "^10.0.27",
        "@keystonejs/adapter-mongoose": "^8.1.0",
        "@keystonejs/app-admin-ui": "^6.0.0",
        "@keystonejs/app-graphql": "^5.1.7",
        "@keystonejs/app-next": "^5.1.2",
        "@keystonejs/app-static": "^5.1.2",
        "@keystonejs/auth-password": "^5.1.7",
        "@keystonejs/field-content": "^6.0.0",
        "@keystonejs/fields": "^10.0.0",
        "@keystonejs/fields-markdown": "^5.1.10",
        "@keystonejs/fields-wysiwyg-tinymce": "^5.2.7",
        "@keystonejs/file-adapters": "^6.0.2",
        "@keystonejs/keystone": "^9.0.0",
        "@keystonejs/list-plugins": "^5.1.4",
        "@keystonejs/oembed-adapters": "^5.1.4",
        "@tinymce/tinymce-react": "^3.6.0",
        "@zeit/next-sass": "^1.0.1",
        "apollo-boost": "^0.4.8",
        "apollo-cache-inmemory": "^1.6.6",
        "apollo-client": "^2.6.9",
        "apollo-fetch": "^0.7.0",
        "apollo-link-http": "^1.5.17",
        "apollo-upload-client": "^13.0.0",
        "app-root-path": "^3.0.0",
        "axios": "^0.19.2",
        "connect-mongo": "^3.2.0",
        "cron": "^1.8.2",
        "cross-env": "^7.0.0",
        "date-fns": "^1.30.1",
        "disqus-react": "^1.0.7",
        "dotenv": "^8.2.0",
        "express": "^4.17.1",
        "express-fileupload": "^1.1.6",
        "express-session": "^1.17.1",
        "graphql": "^14.6.0",
        "graphql-tag": "^2.10.3",
        "isomorphic-unfetch": "^3.0.0",
        "jsonwebtoken": "^8.5.1",
        "lodash": "^4.17.15",
        "loglevel": "^1.6.7",
        "moment": "^2.24.0",
        "multer": "^1.4.2",
        "next": "^9.3.6",
        "next-with-apollo": "^5.0.1",
        "node-cron": "^2.0.3",
        "node-fetch": "^2.6.0",
        "node-sass": "^4.14.1",
        "normalize-scss": "^7.0.1",
        "ramda": "^0.27.0",
        "react": "^16.13.1",
        "react-apollo": "^3.1.5",
        "react-dom": "^16.13.1",
        "reading-time": "^1.2.0",
        "require-directory": "^2.1.1",
        "sanitize-html": "^1.23.0",
        "string-strip-html": "^4.4.3",
        "ts-node": "^8.10.1",
        "typescript": "^3.8.3",
        "uuid": "^7.0.2",
        "validator": "^13.0.0",
        "winston": "^3.2.1"
      },
      "devDependencies": {
        "@babel/plugin-proposal-optional-chaining": "^7.9.0",
        "@types/express-fileupload": "^1.1.3",
        "@types/node": "^13.13.5",
        "@types/react": "^16.9.35",
        "@types/winston": "^2.4.4",
        "babel-cli": "^6.26.0",
        "babel-preset-env": "^1.7.0",
        "cz-conventional-changelog": "3.2.0",
        "eslint-plugin-react-hooks": "^4.0.0",
        "pino": "^6.2.1",
        "pino-pretty": "^4.0.0"
      },
      "config": {
        "commitizen": {
          "path": "./node_modules/cz-conventional-changelog"
        }
      }
    }




Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
gautamsicommented, May 13, 2020

I have had this issue when you try to load require('dotenv').config(); in index.js. I had to put this in each server file I wanted. something wired with keystone cli. I remember I never faced this when i used custom server which is loaded by node directly and not by keystone cli.

you can try add this as node options.

change yarn dev script to like this

"dev": "cross-env PORT=4000 NODE_ENV=development NODE_OPTIONS=\"-r dotenv/config\" DISABLE_LOGGING=true keystone dev",
1reaction
Vultrazcommented, May 13, 2020

Seems the dotenv docs say the -r solution is preferred if you’re using import 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

env file variables are cached · Issue #75 - GitHub
env file is a dependency requiring a new build react-native-dotenv cannot possibly get it to rebuild when the env file changes. I think...
Read more >
Environment Variables Refresh - MSDN - Microsoft
The environment variables are cached when a process starts. Unless the process itself changes them they will not be visible until the process ......
Read more >
Environment Variable caching in Power Automate
In this blog post, I will explain the current problem with Environment Variable caching in Power Automate flows and how to work around...
Read more >
node.js - Nodemon Env Variables Cache - Stack Overflow
I'm running into an issue where the environment variable keys being processed by my node app are old and have since been updated....
Read more >
Caching of path environment variable on windows?
The changes are not dynamic. When you change environment variables for Windows then other processes need to be restarted in order for them ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found