Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all the commands available in package.json?

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.

like image 606
igauravsehrawat Avatar asked Jan 06 '20 14:01

igauravsehrawat


People also ask

How do I list all npm scripts?

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.

What command can you run to see which scripts are in your package json with npm CLI?

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.

What package JSON file contains?

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.


2 Answers

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
like image 98
k0pernikus Avatar answered Oct 01 '22 12:10

k0pernikus


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 🔥!

like image 20
user1949536 Avatar answered Oct 01 '22 12:10

user1949536