title: How to check if npm script exists?
body: <p>I am creating a bash script which runs through each of my projects and runs <code>npm run test</code> if the <code>test</code> script exists.</p>
  
  <p>I know that if I get into a project and run <code>npm run</code> it will give me the list of available scripts as follows:</p>
  
  <pre><code>Lifecycle scripts included in www:
    start
      node server.js
    test
      mocha --require @babel/register --require dotenv/config --watch-extensions js **/*.test.js
  
  available via `npm run-script`:
    dev
      node -r dotenv/config server.js
    dev:watch
      nodemon -r dotenv/config server.js
    build
      next build
  </code></pre>
  
  <p>However, I have no idea how to grab that information, see if <code>test</code> is available and then run it.</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>#!/bin/bash
  
  ROOT_PATH="$(cd "$(dirname "$0")" &amp;&amp; pwd)"
  BASE_PATH="${ROOT_PATH}/../.."
  
  while read MYAPP; do # reads from a list of projects
    PROJECT="${MYAPP}"
    FOLDER="${BASE_PATH}/${PROJECT}"
    cd "$FOLDER"
    if [ check here if the command exists ]; then
      npm run test
      echo ""
    fi
  done &lt; "${ROOT_PATH}/../assets/apps-manifest"
  </code></pre>