Useful npm Tips and Tricks that Developers Should Know

U

This is a collection of npm tips and tricks that every developer should know. If your work involves working with npm packages, you should find these npm commands useful.

Published in: Javascript - Code - Best of the Web

Node Package Manager, or npm, is a tool to install and manage JavaScript packages in your project. And if you have Node installed on your computer, you have already have npm as well.

NPM Tips and Tricks

npm Commands You Should Know

This is not a tutorial for learning npm, the official docs are good place to get started, but a collection of tips and tricks that will help you do more with the npm utility.

Let’s jump right into the list of useful commands:

Instantly run packages without installing

The NPM registry is a treasure trove for finding packages that do useful stuff and aren’t just for programmers.

For instance, the speed-test package shows the speed of your internet connection. The emoj packages helps you search for emojis from the terminal. And wifi-passwords is a simple way to know the password of your current WiFi network.

You can run these utility packages directly from the command line without installing them using the npx command.

npx speed-test
npx emoj unicorn
npx public-ip-cli
npx wifi-password-cli

Install npm packages faster

You’ve probably used npm install to install packages, and dependencies, in the local node_modules folder of a project. Replace this command with npm-ci and you’ll be able to install packages significantly faster.

npm ci

If a node_modules folder is already present, it will be automatically removed before npm ci begins to install packages.

Recover space

If you have been working with npm packages for some time, the various node_modules folders on the disks could be consuming several gigabytes of space. The very useful npkill finds all node_modules folders on your system and lets you delete them interactively.

npx npkill

Quickly download a Git repository

Most developers use the git clone command to download a Git repository. However, this also downloads the entire git history making the process slower. The degit package can download the latest commit to the master branch locally and you need not specify the full Github URL.

npx degit username/repo
npx degit labnol/apps-script-starter

List installed packages

Generate a list of all npm packages that are installed on the system with global scope. Remove the -g flag to list only packages installed in the current project directory.

npm ls --depth=0
npm ls -g

Find unused dependencies

The depcheck command will list all the npm packages that are not used in the project based on the dependencies in package.json.

npx depcheck

Use the command npm uninstall <package-name> to uninstall any unused package.

Find outdated dependencies

Get a list of all outdated packages in your current project. This command checks every single module listed in the package.json file and compares it with the latest version available in the NPM registry.

Add the -g flag to get all outdated packages that are installed globally on the system.

npm outdated
npm outdated -g

Update the package versions

The ncu command will update the package.json file with the latest version of the packages listed in the dependencies and devDependencies sections.

Or use the npm-check -u command to update packages to their latest version in interactive mode.

npm-check
npm-check -u
ncu -u

Remove extra packages

Use the prune command to remove all packages that are installed locally but not listed in the package.json file. If the —dry-run flag is used then no changes will actually be made.

npm prune

Alternatively, you can remove the node_modules folder and run npm ci again.

Find vulnerable packages

Run the audit command to check for vulnerabilities in the packages listed in the dependencies and devDependencies sections. Add the fix flag to automatically apply the fixes, if any.

npm audit
npm audit fix

Useful NPM Package Websites

  • bundlephobia.com - Upload your package.json file and get an idea of how much it would cost (size-wise) to install the dependencies.
  • diff.intrinsic.com - Compare any two versions of a npm package and know which files have changed in the update.
  • npmtrends.com - Compare the relative popularity of packages across the npm registry based on the number of downloads.
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Javascript - Code - Best of the Web

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube