Keystone: Env variables appear to be being cached

Created on 13 May 2020  路  7Comments  路  Source: keystonejs/keystone

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"
        }
      }
    }




Most helpful comment

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",

All 7 comments

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",

Seems the dotenv docs say the -r solution is preferred if you're using import 馃

@gautamsi , @Vultraz -- Unfortunately that didn't seem to work

    // package.json
"dev": "tsc && cross-env NODE_ENV=development NODE_OPTIONS=\"-r dotenv/config\" DISABLE_LOGGING=false  keystone --entry=tsout/index.js dev --port=4545",
// .env
PUBLIC_DIR=public



md5-a042db4ae97fe589ce78f35fe0e7e927



// index.ts
log('PUBLIC_DIR', process.env.PUBLIC_DIR);

traces out

info: PUBLIC_DIR: /public

/public was the value it had before (yesterday). It's not just a matter of the leading / -- other values persist that are completely different strings.

@ra-external did you get this resolved? I've not seen any other issues of this and I'm not sure how to help.

@MadeByMike -- No, I posted it on Stack Overflow, as well in the issue for DotEnv, on the forum fo the IDE I use, and the consensus seemed to be that it was Keystone that was caching it somehow, although someone also suggested it might have something to do with cross-env in the dev script

"dev": "tsc && cross-env NODE_ENV=development NODE_OPTIONS=\"-r dotenv/config\" DISABLE_LOGGING=false keystone --entry=tsout/index.js dev --port=4545",

I haven't had to change any ENV variables in two weeks -- it seems to work fine with _adding_ them -- so I had just let it sit, hoping that an answer would 'appear'. I agree that it's strange, but it was reproducible, that is to say, it persisted for days despite various workarounds.

Strange. Keystone doesn't do any kind of caching other than what might be provided through Express or something like Next.js if you're using that for an app.

Certainly nothing that I imagine could cause this. Is it only happening in your local environment or can you reproduce it on a different host?

Only my local environment. If you're certain that it's not Keystone, I'll close this. This is the only project where it's happened. The strikingly odd thing about it is that it was easily and consistently reproducible, and lasted (still lasts?) for weeks.

Was this page helpful?
0 / 5 - 0 ratings