0

I want to fail an npm script if an environment variable isn't set. Is there a way to do this easily? I would like to do this from the package.json not the actual script.

I also need this to be cross environment (AKA it should be able to run on *nix and Windows).

JGleason
  • 3,067
  • 6
  • 20
  • 54
  • Possible duplicate of [How can I check if an environment variable is set in Node.js?](https://stackoverflow.com/questions/30047205/how-can-i-check-if-an-environment-variable-is-set-in-node-js) – ceejayoz Feb 26 '19 at 17:15
  • 1
    No that shows how to get a env var in the script I need to check it in in the package.json – JGleason Feb 26 '19 at 18:04
  • Put it in the script that `package.json` is running. Barring that, perhaps something from https://stackoverflow.com/questions/307503/whats-a-concise-way-to-check-that-environment-variables-are-set-in-a-unix-shell will do the trick. – ceejayoz Feb 26 '19 at 18:07
  • 1
    That won't work because I need the env var to run node – JGleason Feb 26 '19 at 18:19
  • 1
    NM that question looks to use SH but I need this to run on windows and *nix – JGleason Feb 26 '19 at 18:20
  • You should mention these sorts of things in your question... and npm itself requires Node, so that doesn't really make sense. – ceejayoz Feb 26 '19 at 18:31
  • 1
    Doesn't change the fact it isn't a duplicate – JGleason Feb 26 '19 at 19:26
  • 1
    Also here is an example what if I want a different file location for a npm script based on env for https://nodejs.org/api/cli.html#cli_diagnostic_report_filename_filename. There are a million little reasons which is why I asked the question – JGleason Feb 26 '19 at 19:30
  • 1
    Again, including these sorts of details and examples of what you're trying to do is helpful in a question. I'm still unclear as to why you can't have a script in `package.json` like `do-something.js`, which could check `process.env.FOO` and error out if unset, and do the stuff that relies on `process.env.FOO` if it *is* set. – ceejayoz Feb 26 '19 at 19:40

0 Answers0