0

So I am looking to create a script in the package.json that runs either start:dev or start:prod if ENV in the .env file is set to dev or prod and by default - if nothing is set start:dev should run.

I am unsure how to check environment variables inside a npm run script.

What I want

Something like:

if ENV === dev 
  yarn start:dev 
else if ENV === prod 
  yarn start:prod 
else 
  yarn start:dev

Any ideas?

EMX
  • 6,066
  • 1
  • 25
  • 32
TheWebs
  • 12,470
  • 30
  • 107
  • 211

1 Answers1

1

This is the if/elif/else you are looking for : (extrapolate this example to your own needs)

#!/usr/bin/env bash
if [ "$ENV" = "DEV" ]
then
    echo "Development Mode"
elif [ "$ENV" = "PROD" ]
then
    echo "Production Mode"
else
    echo "ENV : has not been set yet..."
fi

Its possible to then use the .sh from the package.json scripts (example) :

"scripts": {"start": "./checkEnv.sh"}

... dont forget permissions : chmod +x ./checkEnv.sh

EMX
  • 6,066
  • 1
  • 25
  • 32
  • While this is a good starting ground, I didnt know you could do this, how can I then call start:dev or start:prod? I assume I cannot call npm scripts from in the sh script? would the sh script return something? or ... ?? – TheWebs Aug 30 '17 at 16:41
  • @TheWebs : by setting the ENV variable to DEV or PROD you will just have to do `npm start` , and it will run the .sh and it will do whatever you have under the matching conditional statement. just change the `echo`'s for the `yarn start:dev` / `yarn start:prod` . I hope you got it now :) – EMX Aug 30 '17 at 16:56