A package.json
can have lot of commands and common ones are npm start
, npm test
but there are generally more commands.
Is there a way to list all the commands?
Currently I use less package.json
but it has too much noise to show.
Listing the scripts available in a package. json file is easy: just go to the root directory of your project and type npm run in the terminal. Then run the ntl command in the project folder. You'll get a list of available scripts, with the option of selecting one of them to run.
Introduction. You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts.
Your package. json holds important information about the project. It contains human-readable metadata about the project (like the project name and description) as well as functional metadata like the package version number and a list of dependencies required by the application.
You can use:
npm run
to list all commands. (Yarn also features similar functionality on yarn run
.)
This behavior is made explicit in the help page of npm help run
:
This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts.
To get a quick overview if you have jq installed:
jq .scripts package.json
You can still pipe that subset to less
if you need to
jq .scripts package.json | less
Furthermore, you can use tab completion via npm-completion and then you should see a list of possible commands when hitting tab and completes the commands if there is only one option left.
You can set it up temporarily via
source <(npm completion)
Depending on your terminal, you can make it persistent behavior by adding to your relevant config file:
npm completion >> ~/.bashrc
npm completion >> ~/.zshrc
Here's a minimalist approach using only node!
List only the available commands.
node -e "console.log(Object.keys(require('.' + require('path').sep + 'package.json').scripts || {}))"
List all the available commands and their target.
node -e "console.log(require('.' + require('path').sep + 'package.json').scripts || {})"
Taking it one step further, you can turn this into a run command!
In package.json
add it to the scripts.
Example:
"scripts": {
"ls": "node -e \"console.log(require('.' + require('path').sep + 'package.json').scripts || {})\""
}
Now you can npm run ls
🔥!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With