npm
npm init
npm init, generate package.json, initial the setting, figure out the entry point
npm init -y, create package.json without step by step inputs, default entry point is index.js
npm install
all packages should be installed locally
- node_modules in the root directory of the project
A package should be installed globally when it provides an executable command that you run from the shell (CLI), and it’s reused across projects. e.g create-react-app , nodemon npm e.t.c
- {prefix}/lib/node_modules/
npm install, install the dependencies listed in the package.json and package-lock.json
npm install --save-dev, set "dev" to be true for the installed dependency in package-lock.json, which is a development-only package, such as grunt, is added in devDependencies section in package.json
npm install or npm install --save, not set "dev" for the installed dependency in package-lock.json, which is a regular dependency package, is added in dependencies section in package.json
npm install -g <package-name>, install the package in the system
^, npm will pull in the highest version of the package it can find where the only the major version has to match
npm install underscore@1.9.0, install a specific version of the package
package.json and package-lock.json
package.json contains package name, and the semver that should be used, not guarrantee the installed libraries having the exactly same version
package-lock.json specifies a version, location and integrity hash for every module and each of its dependencies, the install it creates will be the same, every single time
- should use package-lock to ensure a consistent install and compatible dependencies
- SHOULD commit your package-lock to source control
npm list
npm list <packageName>, list the information of a package
npm list --global, list global packages
npm help list, show the help documents of list
npm list --depth=3, max display depth of the dependency tree
npm uninstall
npm uninstall, uninstall the local packages
npm uninstall -g, unstall the global packages
Update packages
npm outdated, check if there’s an update
npm update <packageName>, update a package
npm search
npm search <packageName>, search the similar packages
Update npm
npm install npm@latest -g
npm -v, check the npm version
npm cache
When npm installs a package it keeps a copy, so the next time you want to install that package, it doesn’t need to hit the network. The copies are cached in the .npm directory in your home path
npm cache clean --force, clean the cached copies
npm audit
npm audit, check security vulnerabilities
npm audit fix, fix the security vulnerabilities
npm config
npm config list, gives the information about our install
npm config ls -l, show all defaults
npm config get prefix, get the current global location
npm config set prefix, change the default global install locatioin
Aliases
npm i, npm install
npm i -g, npm install --global
npm un, npm uninstall
npm up, npm update
npm t, npm test
npm ls, npm list
npm ll or npm la, print additional package information while listing modules
Reference