An Introduction to NPM Package Manager
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.json
file for your project. It prompts for project details like name, version, description, etc.
- Use
npm init -y
to skip prompts and generate a defaultpackage.json
.
npm init
ShellScript2. Installing Packages
- Installs all dependencies listed in the
package.json
file into thenode_modules
folder.
npm install (or npm i)
ShellScript- Installs a specific package and adds it to your
dependencies
inpackage.json
.
npm install <package-name>
ShellScript- Installs a package and adds it to
devDependencies
inpackage.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>
ShellScript3. Removing Packages
- Removes a package from
node_modules
and updates thepackage.json
file.
npm uninstall <package-name>
ShellScript- Removes a globally installed package.
npm uninstall -g <package-name>
ShellScript4. Updating Packages
- Updates all outdated packages listed in
dependencies
anddevDependencies
.
npm update
ShellScript- Updates a specific package to its latest version.
npm update <package-name>
ShellScript5. 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_modules
folder.
npm prune
ShellScript6. Running Scripts
- Executes a custom script defined in the
scripts
section of yourpackage.json
file.
npm run <script-name>
ShellScript"scripts": {
"start": "node app.js",
"test": "jest"
}
JSONTo run:
npm run start
npm run test
ShellScript- Shortcut for
npm run start
, if astart
script is defined.
npm start
ShellScript7. Cache Management
- Clears the npm cache to resolve potential issues.
npm cache clean --force
ShellScript8. 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>
ShellScript9. 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>
ShellScript10. 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
ShellScript