Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

nps

> 包管理
Open source

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json

1.5K stars0 likes1 views
WebsiteGitHub

About

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json

Sponsorship

NPS is being sponsored by the following tool; please help to support us by taking a look and signing up for a free trial.

nps

All the benefits of npm scripts without the cost of a bloated package.json and limits of json

nps is short for npm-package-scripts

What happened to p-s?

[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![Dependencies][dependencyci-badge]][dependencyci] [![version][version-badge]][package] [![downloads][downloads-badge]][npm-stat] [![MIT License][license-badge]][license] [![PRs Welcome][prs-badge]][prs] [![Donate][donate-badge]][donate] [![Code of Conduct][coc-badge]][coc] [![Roadmap][roadmap-badge]][roadmap] [![Examples][examples-badge]][examples] ![nps friendly][nps-badge]

The problem

Even though npm scripts have a ton of advantages ([learn more][scripts-advantages]), it can grow into an [unmaintainable mess][mess] in your package.json file. Part of the problem is we're configuring scripts in json which has fundamental issues (like no comments).

This solution

nps is a package that solves this problem by allowing you to move your scripts to a package-scripts.js file. Because this file is a JavaScript file, you can do a lot more with your project scripts. Here's an example of a package-scripts.js file:

…

Or in case you prefer YAML, here's an example of how that would look in a package-scripts.yml file:

yaml
scripts:
  default: node index.js
  lint: eslint .
  test:
    # learn more about Jest here: https://kcd.im/egghead-jest
    default: jest
    watch:
      script: jest --watch
      description: run in the amazingly intelligent Jest watch mode
  build:
    default: webpack
    prod: webpack -p
  validate: concurrent "nps lint" "nps test" "nps build"

To use nps, it's recommended that you either install it globally (npm i -g nps) or add ./node_modules/bin to your $PATH (be careful that you know what you're doing when doing this, find out how here).

Then you can run:

bash
nps help

Which will output:

…

You can also use the help command with a script name

bash
nps help test.watch

Which will output the details of the script test.watch:

bash
test.watch - run in the amazingly intelligent Jest watch mode - jest --watch

Now, to run a script, you can run:

bash
nps lint
nps test.watch
# etc.

But the fun doesn't end there! You can use a prefix:

bash
nps b # will run the build script
nps help b # will display help for the build script

And these prefixes can go as deep as you like!

bash
nps b.p # will run the production build script

Cool stuff right? And there's more on [the roadmap][roadmap].

Also check out the [examples][examples]. You'll find some good stuff in there (including how to deal with windows and other cross-platform issues).

Note: If you don't like installing things globally and don't want to muck with your $PATH (or don't want to require that your co-workers or project contributors to do so), then you can add a single script to your package.json. We recommend that you use the start script because it requires less typing:

package.json

json
{
  "scripts": {
    "start": "nps"
  }
}

You don't have to use the start script if you don't want. Note that if you're writing a node application, you're likely using start for starting your server. In that case, you can create a default script which will be run when nps is run without arguments (so effectively it'll work just the same). But if you'd prefer, you can use whatever you wish. For example you could easily create a nps script and do: npm run nps b.

Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and should be installed as one of your project's devDependencies:

bash
npm install --save-dev nps

global installation

You can install this module globally also (this is recommended):

bash
npm install --global nps

From here you can use nps on the command line via one of the installed aliases: nps or nps.

If you do this, you may also be interested in installing the shell autocompletion script. See more about this below.

Getting started

If you're already using npm scripts, you can get up and going really quickly with the init command:

./node_modules/.bin/nps init

or

./node_modules/.bin/nps init --type yml

This will use your package.json scripts to generate a package-scripts.js (respectively a package-scripts.yml) file and update your scripts to utilize the nps binary.

API

CLI

Commands

help

If you have a help script, then your help script will be run. Otherwise, this will output the help.

Note: you can do this with nps --help, but if you're using the start script in your package.json this allows you to run npm start help rather than npm start -- --help

init

As indicated above, this will migrate your npm scripts to package-scripts.

completion
bash
nps completion >> <your-bash-profile-file>

Normally <your-bash-profile-file> will be ~/.bash_profile, ~/.bashrc, or ~/.zshrc.

Note: you should probably only do this if you have the package installed globally. In that case you should probably also normally use the nps alias rather than nps because it's easier to type.

Note: for zsh support, you must enable zsh's bash completion script compatibility mode.

CLI options

-h, --help

Will print out the help you see above (the available scripts are colored and come from the config specified/default config).

-s, --silent

By default, nps will log out to the console before running the command. You can add -s to your command to silence this.

--no-scripts

By default, the script's command text will log out to the console before running the command. You can add --no-scripts to prevent this.

-c, --config

Use a different config

nps -c ./other/package-scripts.js lint

Normally, nps will look for a package-scripts.js file and load that to get the scripts. Generally you'll want to have this at the root of your project (next to the package.json). But by specifying -c or --config, nps will use that file instead.

-l, --log-level

Specify the log level to use

-r, --require

You can specify a module which will be loaded before the config file is loaded. This allows you to preload for example babel-register so you can use all babel presets you like.

scripts

To run a script, you simply provide the name of the script like so:

bash
nps cover

And you can run multiple scripts in series by simply adding more space-separated arguments.

bash
nps cover check-coverage

And you can pass arguments to scripts by putting the scripts in quotes:

bash
nps "test --cover" check-coverage
-y, --help-style

By default, nps will dump a very long help documentation to the screen based on your package-scripts.js file. You can modify this output with one of three help-style options:

all gives you the normal default output:

bash
nps help "--help-style all"

scripts will give you only the help information built from your package-scripts.js file

bash
nps help "--help-style scripts"

basic will give you only the name and description of the scripts from your package-scripts.js file

bash
nps help "--help-style basic"

CLI Configuration File

Some of the options accepted by CLI can also be provided in a .npsrc or .npsrc.json JSON configuration file. It will search upwards starting in the directory the nps command was invoked from.

The accepted options are:

  • require
  • config

The other options can be provided in the specified configuration file (or the default package-scripts.js) once it is loaded, but these options need to be provided in order to find and parse the configuration file.

This is can be useful when you have a reqular set of options you need to pass to nps, especially when using series.nps() or concurrent.nps() from nps-utils.

json
{
  "require": "ts-node/register/transpile-only",
  "config": "package-scripts.ts"
}

That's all for the CLI.

package-scripts.js

Remember, this file is JavaScript, so you can write functions to make things more simple! See other/EXAMPLES.md for examples of cool things you can do with this.

nps expects to your package-scripts.js file to module.exports an object with the following properties:

scripts

This can be an object or a function that returns an object. See the annotated example below for what this object can look like (and different ways to run them):

…
bash
nps k # runs nps kebab-case

options

This object is used to configure nps with the following options:

silent

Setting this to true will prevent nps from outputting anything for your script (normally you'll get simple output indicating the command that's being executed). This effectively sets the logLevel to disable.

logLevel

This sets the logLevel of nps.

ENV variables

LOG_LEVEL

By setting LOG_LEVEL environment variable you can control the log level for nps

Log level

Log levels available:

  • error - errors only
  • warn - errors and warnings only
  • info - info, errors, and warnings (default)

Badge

Congratulations your repo is nps-friendly. Time to flaunt it! Add the nps-friendly badge to your README using the following markdown:

markdown

Your badge will look like this: It may also make sense to change your README.md or CONTRIBUTING.md to include or link to the nps project so that your new contributors may learn more about installing and using nps.

FAQ

How do I do ___ ?

Have you looked at the examples in [other/EXAMPLES.md][examples]?

Why npm start?

Just to be clear: You do not have to use the start script. You can use whatever you like. But I recommend using the start. [npm scripts][npm scripts] are generally run with npm run <script-name>. There are some exceptions to this. For example:

  1. npm run test === npm test === npm t
  2. npm run start === npm start

So, while you could use a script called script and run npm run script build, I just think it reads more clearly to just use the start script and run npm start build. It's also nice that it's fewer things to type. You could also use the test script and then type even less: npm t build, but thats just... odd.

Note, often servers are configured to run npm start by default to start the server. To allow for this case, you can provide a default script at the root of your scripts which will be run when npm start is run without any arguments. Effectively this will allow you to have a script run when npm start is executed.

Resources / Tutorials

  • [Pull out npm scripts into another file with nps][video] by Elijah Manor (5:53)

Inspiration

This was inspired by [a tweet][tweet] by [@sindresorhus][sindre].

Thanks

Big thank you to [@tmpvar][tmpvar] for giving up the name nps! The original nps is now called npmsearch-cli.

Related Packages

  • [nps-utils][nps-utils] - a collection of utilities to make cross-platform scripts and many other patterns (like running concurrent/parallel scripts)
  • nps-i - interactive mode for nps

Other Solutions

  • [scripty][scripty] has a solution for this problem as well. The reason I didn't go with that though is you still need a line for every script (one of the pains I'm trying to solv

Issues· 33 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptautomationclinpmnpm-scripts

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category包管理
PricingOpen source

> Related tools

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image