Installing npm
- 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
- Reinstall
npm i
Useful npm run setting
- Here are some common and useful npm run scripts you can define in your
package.json
file
Prettier
npm run prettier:check
: Checks if files are formattednpm run prettier:write
: Formats the code in the entire project
"scripts": {
"prettier:check": "prettier --check .",
"prettier:write": "prettier --write ."
}
Jest
npm run test
: Runs all the testsnpm run test:watch
: Watches for changes and reruns the testsnpm run test:coverage
: Runs tests and generates a code coverage report
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
}
ESLint
npm run lint
: Lints all files in your project.npm run lint:fix
: Automatically fixes fixable linting errors.
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix"
}