What is npm?
- Packages are structured to enable you to keep track of dependencies and versions.
npm consists of three distinct components:
the website
The website is the primary way developers discover packages, set up profiles, and manage other aspects of their npm experience.
the registry
The registry is a large database of information about packages.
the Command Line Interface (CLI)
The CLI runs from a terminal. This is how most developers interact with npm.
How to Install npm & Manage npm Versions
- Installing npm from the Node.js site
- update npm
- npm -v
- npm install npm@latest -g
- npm install npm@next -g
- update npm
- Using a Version manager to install node.js and npm
- Since npm and node.js products are managed by different entities, updates and maintenance can become complex. Also, the Node.js installation process installs npm in a directory that only has local permissions. This can cause permissions errors when you attempt to run packages globally.
How to Prevent Permissions Errors
If you see an EACCES error when you try to install a package globally, read this chapter. This error can often be avoided if you change the directory where npm is installed. To do this, either:
1. Reinstall npm with a version manager (recommended).
2. Change npm’s default directory manually.
How to install Local Packages
- Installing a Package
- npm install < package_name >
- Which Version of the Package is Installed?
- If there is no package.json file in the local directory, the latest version of the package is installed.
- If there is a package.json file, npm installs the latest version that satisfies the semver rule declared in package.json.
How to use Semantic Versioning
- If you were starting with a package 1.0.4, this is how you would specify the ranges:
- Patch releases: 1.0 or 1.0.x or ~1.0.4
- Minor releases: 1 or 1.x or ^1.0.4
- Major releases: * or x
Working with package.json
- Requirements
- Name
- All lowercase
- One word,no spaces
- Dashes and underscores allowed
- Version
- In the form of x.x.x
- Follows semver spec
- Name
- Createing a package.json
- Run a CLI questionnaire
- Create a default package.json
- Use Customize the package.json questionnaire
- Create a custom .npm-init.js in ~/
Specifying Dependencies
Dependencies
These packages are required by your application in production
devDependencies
Theses packages are only needed for development and testing
npm install
Default add to dependencies
npm install –save
Reinstall can not cover devDenpendencies
npm install –save-dev
Reinstall can cover denpendencies
How to Update Local Packages
- npm update [-g] [< pkg >]
- npm outdated
How to Uninstall Local Packages
- npm uninstall < package > [–save][–save-dev]
It will remove package define in package.json whether it in devDenpendenices Object or not in Denpendenices by use –save or –save-dev
How to Install Global Packages
- There are two ways to install npm packages: locally or globally. Choose which kind of installation to use based on how you want to use the package.
- If you want to use a package as a command line tool, then install it globally. This way, it works no matter which directory is current. This is the choice you would use if you were installing grunt, for example.
- If you want to depend on the package from your own module, then install it locally. This is the choice you would use if you are using require statements, for example.
- npm install -g < package-name >
How to Update Global Packages
- npm update -g
- npm outdated -g
How to uninstall global packages
- npm uninstall -g < package_name >