20 Useful Npm Tips and Tricks
Shortcuts
Save time typing
npm i- is a shortcut fornpm installnpm t- is a shortcut fornpm test-g- to save a global dependency-D- to save as development dependency:npm install packagename --save-dev
Info
npm info webpack dist-tags
{
latest: '5.6.0',
legacy: '1.15.0',
'webpack-2': '2.7.0',
next: '5.0.0-rc.6',
'webpack-3': '3.12.0',
'webpack-4': '4.44.2'
}
What version of a package is the latest.
npm ci
npm ci bypasses a package’s package.json to install modules from a package’s lockfile. This ensures reproducible builds - you are getting exactly what you expect on every install. Providing a consistent and fast experience for developers using CI/CD in their workflow.
Tagged version
npm install some-package@nextTo install not the “latest” version, but the version tagged by “next”
Version shortcuts
// 1.0.0
npm version patch
// 1.0.1
npm version minor
// 1.1.0
npm version major
// 2.0.0To increase the version.
pre- and post- hooks
Special lifecycle scripts which can be used to run scripts automatically in sequence.
{
"scripts": {
"pretest": "eslint .",
"test": "jest"
},
}List installed packages
npm listList packages
npm list -gList packages installed globally
Check outdated packages
npm outdated
Package Current Wanted Latest Location
@babel/core 7.4.0 7.12.8 7.12.8 projectname
@babel/preset-env 7.4.2 7.12.7 7.12.7 projectname
Test your own packages locally
npm link ...Develop node modules locally using npm link. It enables us to develop our modules and test them in our projects seamlessly.
Silent
The –silent option reduces the output in the Terminal.
{
"scripts": {
"something": "npm run something --silent"
}
}nls
Missing inspector for npm packages.
nls List available npm scripts
nls deps List dependencies table (d for short)
nls view <prop-path> Extract info from package.json (v for short)
nls read <package-name> Print readme file from a dependency (r for short)
nls why <package-name> Identify why a package has been installed (w for short)Example:
npx nls why @babel/core
Who required @babel/core: tools > @babel/core@7.12.3Node Task List
Interactive CLI tool that lists and run package.json scripts.
Run all
Instead of:
npm run clean && npm run build:css && npm run build:js && npm run build:htmlDo:
npm-run-all clean build:*Initialize package
npm initnpm init -yUse -y to to automatically generate your package.
npm config set init-license MITTip: Set config to change for example the default license generated by npm init -y
Scan for vulnerabilities
npm auditScan your app for vulnerabilities:
npm audit fixScan your project for vulnerabilities and automatically install any compatible updates to vulnerable dependencies:
npx
Since npm 5.2.0, npm comes with a tool called npx. It executes a script from your dependencies as node executable.
- npm - the package manager
- npx - the package runner
npm vs npx — What’s the Difference?
The prefix flag
npm start --prefix path/to/your/folderUse the --prefix flag to specify the path:
npm doctor
Check your environments
npm doctornpm-pack
npm pack --dry-runShows which files would be packed when publishing your package to npm.
How to write npm
TL;DR; don’t put it in all caps ever. npm is never capitalized officially.