npm install
Install local packages
npm install <package_name>
: This command will create the node_modules directory in the current directory (if it doesn't already exist) and download the package to that directory. This command installs locally by default.
Which version of the package is installed?
If there is no package.json file in the local directory, the latest version of the package will be installed.
If there is a package.json file, install the latest version that satisfies the semver rules declared in package.json for that package (if any).
Install global packages
npm install -g <package>
: Global installation package.
package.json
npm init
npm init
: This command is used to create a package.json.
npm init --yes
OR npm init -y
: Generate a default package.json from information extracted from the current directory. No questions will be asked during the creation process.
If you already have a package.json file in your directory and you run it npm install
, npm will look at the dependencies in that file and download the latest version that satisfies all of them.
The description in the package.json file helps people find your package in npm search, so having a custom description in package.json is very useful.
It is also possible to fully customize the contents of the package.json file and the questions asked during init. This is done by creating a custom .npm-init.js. By default, npm will look in your home directory. ~/.npm-init.js
dependencies与devDependencies
dependencies and devDependencies specify the packages that the project depends on.
-
dependencies: These packages are required in production.
-
devDependencies: These packages are used for development and testing.
npm install <package_name> --save
The command will add the entry to the dependencies in package.json. npm install <package_name> --save-dev
The command will add the entry to the devDependencies in package.json.
npm update
Update local packages
npm update
: used to update dependent packages. The command needs to be run in the same directory as the package.json file.
Update global packages
npm update -g <package>
: Update global packages. npm update -g
: Update all global packages. npm outdated -g --depth=0
: Find out which packages need to be updated.
npm uninstall
Uninstall local packages
npm uninstall <package>
: Remove a package from the node_modules directory.
npm uninstall --save <package>
: Remove a package from dependencies in package.json.
npm uninstall --save-dev <package>
: Remove a package from devDependencies in package.json.
In actual operation, it is found that the use npm uninstall <package>
will not only delete the package in the node_modules directory, but also delete the information in the dependencies or devDependencies of the package in package.json.
uninstall global packages
npm uninstall -g <package>
: Uninstall global packages.
Summary : Local commands plus -g are global commands.
Reference from: npm
Original link: https://segmentfault.com/a/1190000010001155