- Install and test
- Run the command in a different folder
- Keep NPM dependencies up-to-date
- Control the NPM cache
- Generate random string
- Extra tools
- My NPM blog posts
- Related posts
Install and test
You can install NPM dependencies and run tests with a single command:
$ npm it
If you use the
$ npm cit
Run the command in a different folder
You can use the option
--prefix <path/to/folder> to run NPM command in a particular folder. It works like a cross-platform
cd <path/to/folder>; npm ... combination.
Let's say we are in a larger project, and the server we want to launch is in its own subfolder
Let's say the
sub/folder/package.json has a "start" script
Then we can install the dependencies and launch the app in the
sub/folder all from the root
app/ $ npm --prefix sub/folder install
Keep NPM dependencies up-to-date
- using an external service like my favorite Renovate is my preferred way of keeping NPM dependencies up-to-date, read my Renovate blog posts
- using local tool next-update gives you stats and runs the tests when upgrading each module to ensure the update does not break the project
Control the NPM cache
By default NPM downloads the modules to install into
~/.npm folder (and a similar user folder on Windows). You can change the cache folder using an environment variable
npm_config_cache. You can also change it at run-time by providing
--cache <path/to/folder> argument:
# install NPM dependencies using a local folder ".npm" to
Generate random string
node -p 'crypto.randomBytes(4).toString("hex")'
Pseudo-random using `Math.random()
Here are some useful tools I use every day:
- npm-quick-run lets you run NPM scripts without typing the full script name.
- available-versions fetches the new versions for the given NPM package name and shows them in a nice console table.
- as-a runs a given command, including NPM scripts with additional settings from the secrets file injected as environment variables.
- bin-up Finds an installed tool in node_modules/.bin folders from current up to the git root.
- bahmutov/npm-install is my GitHub action for simple NPM installs (with caching) when using GitHub Actions.
My NPM blog posts
- How I Organize my NPM Scripts
- Do Not Let NPM Cache Snowball on CI
- Subfolders as Dependencies
- Do not use NODE_ENV for staging
- How I publish to NPM
- Shell variables in NPM scripts
- Smaller published NPM modules