npm is the standard package manager for Node.js. It is one of the largest software registries where you can find millions of JavaScript codes commonly known as Packages.
With npm, you can install, update, and manage dependencies for your project.
What are Packages?
Simply packages are bundles of JavaScript code developers have written to solve particular problems. These packages are reusable, you can install them into your project using npm instead of writing the code from scratch.
1. Initialization and Configuration
- Creates a new
package.jsonfile for your project. It prompts for project details like name, version, description, etc. - Use
npm init -yto skip prompts and generate a defaultpackage.json.
npm init
ShellScript
2. Installing Packages
- Installs all dependencies listed in the
package.jsonfile into thenode_modulesfolder.
npm install (or npm i)JavaScriptShellScript
- Installs a specific package and adds it to your
dependenciesinpackage.json.
npm install <package-name>
ShellScript
- Installs a package and adds it to
devDependenciesinpackage.json.
npm install <package-name> --save-dev
ShellScript
- Installs a specific version of a package.
npm install <package-name>@<version>
ShellScript
- Installs a package globally, making it available across your system.
npm install -g <package-name>
ShellScript
3. Removing Packages
- Removes a package from
node_modulesand updates thepackage.jsonfile.
npm uninstall <package-name>
ShellScript
- Removes a globally installed package.
npm uninstall -g <package-name>
ShellScript
4. Updating Packages
- Updates all outdated packages listed in
dependenciesanddevDependencies.
npm update
ShellScript
- Updates a specific package to its latest version.
npm update <package-name>
ShellScript
5. Managing Dependencies
- Displays a tree of all installed dependencies for your project.
npm list
ShellScript
- Lists all globally installed packages.
npm list -g
ShellScript
- Removes unused packages from the
node_modulesfolder.
npm prune
ShellScript
6. Running Scripts
- Executes a custom script defined in the
scriptssection of yourpackage.jsonfile.
npm run <script-name>
ShellScript
"scripts": {
"start": "node app.js",
"test": "jest"
}
JSON
To run:
npm run start
npm run test
ShellScript
- Shortcut for
npm run start, if astartscript is defined.
npm start
ShellScript
7. Cache Management
- Clears the npm cache to resolve potential issues.
npm cache clean --force
ShellScript
8. Package Version Management
- Installs a specific version of a package.
npm install <package-name>@<version>
ShellScript
- Lists all available versions of a package.
npm view <package-name> versions
ShellScript
- Shows detailed information about a package, including its dependencies, versions, and maintainers.
npm info <package-name>
ShellScript
9. Publishing and Sharing
- Publish your package to the npm registry.
npm publish
ShellScript
- Removes a published package from the npm registry.
npm unpublish <package-name>
ShellScript
10. Miscellaneous Commands
- Displays the current version of npm installed on your system.
npm version
ShellScript
- Displays help information for npm commands.
npm help
ShellScript
- Scans your project for security vulnerabilities in dependencies.
npm audit
ShellScript
- Automatically fixes vulnerabilities if possible.
npm audit fix