Installing npm
To use npm, you need Node.js installed on your computer. Why? Because npm comes bundled with Node.js.
- To check if it’s installed, open your command line or terminal and type
node -v
npm -v
Semantic Versioning
This is semantic versioning, and it’s important for managing package updates without breaking your project. Here’s a quick breakdown:
- Major version (1.x.x): Introduces breaking changes.
- Minor version (x.1.x): Adds functionality in a backwards-compatible manner.
- Patch version (x.x.1): Fixes bugs without adding new features.
The ^
symbol allows minor and patch updates. Using ~
would only allow patch updates.
Package Management
- Install packages defined in the
package.json
file
npm install
- Install a package locally
npm install <package_name>
- Install a specific version of a package
npm install <package_name>@<version>
- Install a package globally
npm install <package_name> --global
- Install and save a package as a dependency
npm install --save <package_name>
- Install and save a package as a development dependency
npm install --save-dev <package_name>
- Updating Packages
npm update <package_name>
Handling Global Packages
- List globally installed packages
npm list -g --depth=0
- Find the location of globally installed packages
npm root -g
Dependency Management
- Update installed packages
npm update
- Check for outdated packages
npm outdated
- Uninstall a package
npm uninstall <package_name>
- Remove packages not listed in the
package.json
file
npm prune
Scripts
- Run the
start
script defined inpackage.json
npm start
- Run the
test
script defined inpackage.json
npm test
- Run a custom script defined in
package.json
npm run <script_name>
Publishing
- Initialize a new
package.json
file
npm init
- Publish a package to the npm registry
npm publish
- Update the version number in
package.json
npm version <version>
Miscellaneous
- Search for packages on the npm registry
npm search <keyword>
- Get detailed information about a package
npm info <package_name>
- List installed packages
npm ls
- Get help with npm commands
npm help
Reinstall
- Remove node modules
rm -rf node_modules
- Install
npm i