Skip to content

Reuse NPM scripts with NPM cli arguments

Posted on:March 11, 2024

Did you know that any command line flag you pass to an npm script is visible as an npm_config_ variable? This is very handy for reusing scripts.

For example, adding this to your package.json:

"scripts": {
  "clean": "cd $npm_config_dir && rm -rf dist node_modules"
}

Will make running npm run clean --dir=some/subdir direct the clean operation to the specified directory.

This simplifies your workflow and makes your project scripts more dynamic and adaptable.

References: