Published on

How to write Fallback Command for npm script


Sometimes, we might want to have a fallback command in our npm script.

Here is how to do it:

Just add || between your commands (just like in the programming language). It'll start from the left to right (if the previous command fails)

For example, In my case, I'm using 1Password to manage my app's env secrets.

So when I run npm run dev it needs to fetch env secrets from 1Password using op CLI. However, if the op CLI does not exist then it should fall back to the next command.

    "dev": "op run --env-file='./.env' -- next dev -p 3030 || next dev -p 3030",

Happy secure DX!