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.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 initShellScript2. Installing Packages
- Installs all dependencies listed in the
package.jsonfile into thenode_modulesfolder.
npm install (or npm i)ShellScript- 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-devShellScript- 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_modulesand updates thepackage.jsonfile.
npm uninstall <package-name>ShellScript- Removes a globally installed package.
npm uninstall -g <package-name>ShellScript4. Updating Packages
- Updates all outdated packages listed in
dependenciesanddevDependencies.
npm updateShellScript- 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 listShellScript- Lists all globally installed packages.
npm list -gShellScript- Removes unused packages from the
node_modulesfolder.
npm pruneShellScript6. 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"
}JSONTo run:
npm run start
npm run testShellScript- Shortcut for
npm run start, if astartscript is defined.
npm startShellScript7. Cache Management
- Clears the npm cache to resolve potential issues.
npm cache clean --forceShellScript8. 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> versionsShellScript- 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 publishShellScript- 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 versionShellScript- Displays help information for npm commands.
npm helpShellScript- Scans your project for security vulnerabilities in dependencies.
npm auditShellScript- Automatically fixes vulnerabilities if possible.
npm audit fixShellScript