I like npm scripts. I’m sure real programmers can memorize long strings of command line gibberish without even waking up the full 10% of our brain capacity movies tell me is all we have access to, but I mess those commands up all the time.
Don’t get me wrong- it’s fun to be able to cruise around a system on the command line. You feel like you really know secret stuff. I like that you don’t really even need a monitor to get real work done on a server- just a secure login method and some useful commands. ssh in, check out where you are and where you can go (ls -la), change directories and do it again (cd var/www && ls -la). See something you want to check out? Cat is your friend (cat package.json). See something you want to change? Use Vim (vim package.json). Though be sure to install it first (sudo apt-get vim -y) and be ready to look up the commands just to get around the page (note: I am not a Vim expert- I only use it when I have no other option).
But when it’s time to get to work, it’s really nice to just be able to type ‘npm start’ and have your dev environment spin up. Particularly when you’re a less than stellar typist.
Npm scripts just go in your package.json file- in the appropriately named “scripts” object. They can be extremely simple- for example, a project built with the angular cli automatically sets up a couple: “test”: “ng test”, “start”: “ng serve”. These just make it easier to find those (admittedly already simple) commands. A developer will know they can run “npm start”, but if they’re not familiar with the angular cli, they might not know about “ng serve” right away. Npm scripts work well as aliases- unifying those commands under one umbrella- and one less thing for me to try to remember.
You can extend those simple commands as well. I made a simple edit to my start command: “start”: “ng serve –open”. This passes the ‘open’ flag to ng serve, automatically opening my default browser. There are flags for many other options- check out the docs!
But where Npm scripts have really shined for me is in abstracting away any directory changes and environment settings that may have to be done in order to fire up other aspects of a project. Sure- when you’re just working on the frontend, it’s easy to remember that you run “npm start” from the same directory your package.json resides. But one project I’m working on uses .net core as a backend API. It runs locally, so one option is to fire up the full Visual Studio program, but this feels big and slow compared to VS Code now. Luckily, .net core now comes with a command line interface. I can run the command “dotnet run”, and my backend dev server starts right up.
But there are complications. This executable is not run from the same directory as package.json- it’s technically a completely different project in a completely different folder. An environment variable also has to be set to “Development” in order for it to run properly.
But Npm scripts don’t care! A script will just take your commands and run them. So, my new script is: