Search code examples
node.jsnpmdevelopment-environmentnpm-installproduction-environment

Node/NPM: How to pass arguments with .sh file call


I am using package.json's script block to call postinstall.sh script immediately after yarn install and yarn install --production.

postinstall.sh contains some private npm packages, so we want to download devDependencies of those packages only when the environment is development and we want to ignore when the environment is production.

package.json

  "private": true,
  "scripts": {
    "postinstall": "./postinstall.sh"
  }

postinstall.sh

 # Get the input from user to check server is Production or not
echo
echo "INFO: We are not downloading Development dependencies of following services in Production environment:
echo "WARN: please enter 'yes' or 'no' only."

while [[ "$PRODUCTION_ENV" != "yes" && "$PRODUCTION_ENV" != "no" ]]
do
  read -p "Is this Production environment?(yes/no): " PRODUCTION_ENV
done

if [[ "$PRODUCTION_ENV" == "yes" ]]; then
   ENV='--only=production'
fi

npm install $ENV --ignore-scripts pkg-name

The problem with above script is we don't want any user interaction, so how can I pass an argument from package.json depending on the environment?


Solution

  • And, simple answer to this is "postinstall": "./postinstall.sh \"$NODE_ENV\"". So, final look a like below.

    "private": true,
    "scripts": {
       "postinstall": "./postinstall.sh \"$NODE_ENV\""
    }