Useful npm Commands

Rumman Ansari   Software Engineer   0000-00-00 00:00:00   23 Share
Subject Syllabus DetailsSubject Details
☰ Table of Contents

Table of Content:


Useful npm Commands

Here are a few npm commands that are popularly used:

  • npm Prune

  • npm Search

  • npm Dedupe

  • npm Run

npm Prune

npm prune is a way to clean up your package. During development, you might end up installing packages to try something and later against using it.

Prune is a way to remove all those packages that are not listed as dependencies in the package.json file.

To list all packages that are used by the project as well as the ones installed and not in use,

>npm list --depth 0

Run the prune command to remove/unbuild all packages that were identified as extraneous.

>npm prune
  •  
  •  
npm Search

npm search command searches the registry and package metadata and returns packages matching the search terms.




Syntax: npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...]



Options used to filter and format search results:

  • --no-description: Omits search on package description.

  • --json: Returns results as a JSON array.

  • --parseable: Returns results as rows with tab-separated columns.

  • --long: Displays entire package details across multiple lines. When disabled, results are neatly fit into a single line.

  • searchexclude: Displays options separated by space and filters the search results.

  • Ending a search term with ~ (eg: rea~) Returns all packages starting with "rea".

npm Dedupe

npm installs the entire dependency package tree by default. However, when you install multiple packages for a project, there is a high possibility of same package being installed multiple times, owing to it being an inner dependency for multiple packages.

This behavior slows down the project installation.

This is where Npm dedupe comes to rescue.

"Npm dedupe" searches the local package tree and reduces duplicates by moving dependencies further up the tree.

 

npm Shrinkwrap

npm shrinkwrap locks down the version numbers of installed packages and their descendant packages.

It helps you to use same package versions on all environments (development, staging, production) and also improve download and installation speeds.

You can execute npm shrinkwrap after installing packages using npm install. This will create a new npm-shrinkwrap.json file with information like package version and the descendant packages being used.

NPM Run

While npm is a great package manager, it has the capability to run tasks in a packages lifecycle, making it a great tool for build scripts. It can very well do all that the build tools like grunt and gulp can do, with less maintenance overhead.




>npm run <args>



Arguments in the npm run refer to property in the scripts object configured in package.json file. This will execute the value of the property as a command in the operating system's default shell.

npm Run Example

Consider scripts module in package.json file:

"scripts": {
    "patch-release": "npm version patch && npm publish && git push --follow-tags"
  }

Running "npm patch-release" will update the version in package.json, commit the change, publish the changed package to npm and push the changes to GitHub. Note: "npm run" can be used not only on the globally available commands, but also on the modules installed as dependencies.

 

 

npm Run - Build Example

Here is another example that depicts the usefulness of the npm run command.

"scripts": {
    "build": "...",
    "git-commit": "git add -A . && git commit -a -m 'gh-pages update'",
    "git-push": "git push origin gh-pages --force && git checkout master",
    "deploy": "npm run build && npm run git-commit && npm run git-push"
  },

With this script, deploying your project is as easy as running "npm run deploy".

 

b