NPM, short for Node Package Manager, is a widely-used tool for managing JavaScript packages in a project. It allows developers to install and update packages, as well as manage dependencies and scripts. NPM comes bundled with Node.js, so if you have Node installed on your machine, you automatically have access to NPM as well.
This is not a tutorial for learning npm, the official docs are a good place to get started, but a collection of tips and tricks that will help you do more with the npm utility. Whether you’re a seasoned developer or just starting out, these tips can help you be more efficient and productive in your work with npm.
The NPM registry is a treasure trove for finding packages that do useful stuff and they aren’t just for programmers.
For instance, the speed-test package shows the speed of your internet connection. The emoj package helps you search for emojis from the terminal. And the wifi-passwords package can help you find the password of your current WiFi network.
You can run these utility packages directly from the command line using the npx command.
npx speed-testnpx emoj unicornnpx public-ip-clinpx wifi-password-cliUse the npm view command to get details of any npm package, including the repository URL, the dependencies and the date when the package was last updated.
npm view eslintYou’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 ciIf a node\_modules folder is already present, it will be automatically removed before npm ci begins to install packages.
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 npkillMost 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/reponpx degit labnol/apps-script-starterGenerate 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=0npm ls -gThe depcheck command will list all the npm packages that are not used in the project based on the dependencies in package.json.
npx depcheckUse the command npm uninstall <package-name> to uninstall any unused package.
The unimported package will find all the unused files and dependencies in your JavaScript / TypeScript projects.
npx unimportedGet 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 outdatednpm outdated -gThe npm outdated command will list all packages in your current project that are outdated and a newer version is available. Add the -g flag to list outdated packages that are installed in the global scope.
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 outdatednpm outdated -gnpm-checknpm-check -uncu -uUse 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 be made.
npm pruneAlternatively, you can remove the node\_modules folder and run npm ci again.
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 auditnpm audit fixpackage.json file and get an idea of how much it would cost (size-wise) to install the dependencies.