2

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"
    }
  }
}
Cerulean
  • 5,543
  • 9
  • 59
  • 111
  • 3
    `dotenv` does not watch for changes in the `.env` file, the file is only parsed if you call `config()` or the start of the application if `preload` is used. So how do you reload the config if you did changes to `.env`? – t.niese May 12 '20 at 17:16
  • @t.niese The main file for the project, `/index.ts`, has `require('dotenv').config();` at the top, just after the imports. (I'm using Babel so that I can use 'import' instead of 'require' for all other imports in the project, but I can't imagine that makes a difference). Each time the project is restarted this file is run, so I would assume that dotenv is reloading the .env. I've used dotenv for a very long time, never run into this before. – Cerulean May 13 '20 at 07:16
  • 2
    What app are you using to edit the `.env` file? Sometimes WebStorm's smart save (or whatever it's actually called) feature won't write the changes to disk when you expect – awarrier99 May 13 '20 at 07:22
  • @awarrier99 -- Why it is WebStorm indeed. Just googled 'WebStorm smart save' but didn't see any relevant results -- do you have any more info? – Cerulean May 13 '20 at 07:27
  • 2
    @Cerulean yeah I can't remember for the life of me what it's called. i'm currently digging through my settings to see if I can find it because I remember stumbling across it once – awarrier99 May 13 '20 at 07:29
  • 3
    @Cerulean I'm not entirely sure if this is what I remember seeing in the past, but it could have to do with your Synchronization (Preferences -> Appearance & Behavior -> System Settings) settings (more details [here](https://www.jetbrains.com/help/webstorm/working-with-source-code.html#auto_save) and [here](https://www.jetbrains.com/help/webstorm/system-settings.html)) – awarrier99 May 13 '20 at 07:32
  • Maybe try ensuring that `Safe Write` is turned off and try enabling the option for `Save files automatically if application is idle for _ sec.` and inputting a number there – awarrier99 May 13 '20 at 07:35
  • I also found a similar issue [here](https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000112590-Webstorm-doesn-t-save-changes-on-disk) so maybe try saving the file in another editor to determine whether the issue is definitely with WebStorm – awarrier99 May 13 '20 at 07:37
  • 2
    To me this sounds like a chaching issue. Not sure where, or what, would do it. Since the new values does appear, then it indicates to me that the file is saved correctly, so there has to be some part of the system remembering the old state. Perhaps cross-env, or keystone itself caches something? – Automatico May 13 '20 at 07:41
  • @Automatico -- Indeed, that was my thinking. I don't know anything about `cross-env`-- what is that? I asked on the Keystone forum but only one person responded that they didn't know if KS cached anything. – Cerulean May 13 '20 at 07:50
  • 1
    As far as I know, `cross-env` allows the setting of environment variables in a cross-environment fashion. `NODE_ENV=development` wouldn't be valid syntax on Windows, but `cross-env` allows for its usage. I don't think that this would involve any caching – awarrier99 May 13 '20 at 07:52
  • @Automatico -- `cross-env` is being used extensively by part of the project not written by me (that is, in `node_modules`), but I'm not sure how to track down if that's causing me problems... – Cerulean May 13 '20 at 07:55
  • Btw, any luck with saving the file in a different editor? That way you can at least narrow down whether it's a file-saving or caching issue – awarrier99 May 13 '20 at 08:07
  • @awarrier99 -- I added the old variable back (the one whose value wouldn't change) using Visual Studio Code, and it worked -- on the one hand that would seem to indicate that it might be the editor at fault; on the other hand, it's been a day or so since I removed the old variable and there have been a fair number of commits, pulls, etc, so that might have had an effect as well. – Cerulean May 13 '20 at 12:29
  • @awarrier99 -- Nope, that's not it. Just changed an existing env var in WebStorm, checked it in VSC, it was changed, but in the NodeJS runtime it traces out as the old value. – Cerulean May 13 '20 at 14:59
  • Huh so I guess it is caching then. Not sure this will make a difference, but do you experience the same issue if you use `dotenv`'s preload option? – awarrier99 May 13 '20 at 20:40
  • I tried that, but it doesn't seem to help... – Cerulean May 16 '20 at 15:28

0 Answers0