Show HN: Run NPM Scripts with .envs
npmjs.comI built this package while working on an app that I deployed to production, staging and my local dev environment. I had a couple of scripts (one to deploy, one to migrate the database, etc.) that I wanted to run, but I didn't want to have to maintain one script for each environment doing almost the same but to different endpoints.
So, I built a simple CLI that reads one or more .env files and spawns a new child process running an NPM script from the package, with the environment variables extended with the contents of the files.
It also supports expansion, where one file use the value defined in another file.
Please see the README for more details and usage and consider giving it a spin in your next project if you're faced with a similar problem.
Instead, I highly recommend https://github.com/casey/just .
Add these two lines to the top to use node executables and load the dotenv:
``` set dotenv-load := true export PATH := "./node_modules/.bin:" + env_var('PATH') ```
Thanks for sharing this. Thats sure looks like a great alternative.
Fun fact: I actually lived with Casey (the maintainer) in a co-living space about 7 years ago. Small world ...
I still do not understand the pros of having these scripts in package.json instead of ./scripts/*.sh..
I second tedmiston's point above. It's mainly convenience (running `npm run` which can be auto-completed with a shell integration too), but also discoverability: If you're switching between multiple projects, its nice to know that you can simply look there for the commands that you're able to run.
I especially like the package.json scripts for simple one-liners, if I have something complicated I might also create a ./scripts directory with a script and call that from an npm run-script.
Mainly just the convenience of being able to run `npm run foo` and convenience if you need to interact with any executables installed in the node modules.
I still use Makefiles everywhere in all of my projects. I like knowing I can have a consistent interface to everything with `make build`, `make run`, `make install`, etc.