Skip to main content

Sebastiaan Luca

Using Yarn to create shortcuts for often-used commands

December 21, 2016
1 minute read
This blog post was written more than a year ago. Be wary of any technologies that might have changed and if no better alternatives exist at the time of viewing this post.

I recently came across a blog post that suggested using Composer's composer.json file to keep a reference to your most often used project-specific commands. I've been doing so for quite some time and it might be a matter of personal preference, but I find Yarn (or npm) to be better suited for this.

Photo by Troy Chen on Unsplash

It's a small and opinionated improvement, but instead of typing:

composer run-script serve

We can type:

yarn run serve

Both act as a shortcut and will execute the command we've specified in our composer.json or package.json.

If you've followed along with the previous post where we discussed using bash aliases to quick-start your development environment, we can even shorten this further to:

yr serve

Enabling Yarn shortcuts

To get this to work, add the following to the package.json file in your project's root directory:

"scripts": {
    "serve": "gulp serve",

An additional, but often overlooked benefit to using such shortcuts is we no longer have to specify the full path to a binary project executable like gulp or webpack, not even in the package file. Therefore node_modules/.bin/gulp becomes gulp.

Other use-cases

Here too, your imagination is key. If you notice you're using a command regularly, add a shortcut! Here are some I use on a daily basis:

"scripts": {
    "build": "gulp",
    "watch": "gulp watch",
    "hot": "gulp hot",
    "serve": "gulp serve",
    "vm": "vagrant up && vagrant ssh",
    "echo": "laravel-echo-server start",
    "queue": "php artisan queue:work --queue=high,default,low --timeout=60 --tries=1 --sleep=1",
    "models": "php artisan ide-helper:models --nowrite",
    "rebuild": "php artisan migrate:refresh --seed",
    "dump": "composer dumpautoload",
    "optimize": "composer run-script post-update-cmd",
    "envoy": "vendor/bin/envoy",
    "deploy": "vendor/bin/envoy run deploy",
    "cmd": "php artisan your:command",

Can't remember a shortcut or don't want to open the package file? Type yr to show a list of all commands and Yarn will help you on your way.