Skip to content

Latest commit

 

History

History
81 lines (66 loc) · 3.41 KB

npm.md

File metadata and controls

81 lines (66 loc) · 3.41 KB

NPM (Node Package Manager)

Overview

npm is the world's largest software registry, used extensively for sharing and managing public and private packages. It is an indispensable tool for any Node.js developer, providing a vast library of reusable code that greatly facilitates dependency management.

What is npm?

npm consists of a command-line client that developers use for installing, configuring, updating, and uninstalling Node packages, and an online database known as the npm registry. The registry, accessible via the client, hosts thousands of public and private packages, which are searchable through the npm website.

Installing npm

npm is bundled with Node.js. When you install Node.js from the official Node.js website, npm is automatically installed on your computer.

Check npm version

To verify the installed version of npm, run the following command in your terminal:

npm --version

Managing Packages with npm

npm allows for package management both locally within a project and globally across your entire system.

Installing a Package Locally

npm install <package-name>

This command installs a package within the node_modules directory of your current project, making it available for import or require in your application's code.

Installing a Package Globally

npm install -g <package-name>

Installing a package globally allows you to use it across multiple projects, providing access to its commands from anywhere on your system.

Updating Packages

To update a specific package to the latest version based on the versioning rules in your package.json, use:

npm update <package-name>

To update all packages in your project, simply run:

npm update

Uninstalling a Package

npm uninstall <package-name>

This command removes the package from your node_modules directory and updates the associated entries in your project’s package.json and package-lock.json.

package.json

The package.json file is central to managing your project's dependencies, scripts, versioning, and much more. It's typically located at the root of your Node.js project.

Creating a package.json

npm init

This command initiates an interactive process to create a package.json file. For a quicker setup with default values, you can use:

npm init -y

Dependencies and DevDependencies

"dependencies": {
    "express": "^4.17.1"
},
"devDependencies": {
    "nodemon": "^2.0.4"
}
  • Dependencies are crucial for the application to run.
  • DevDependencies are only required during development and not in production environments.

npm Best Practices

  • Version Control: Always keep package.json and package-lock.json under version control to track changes and ensure consistent installations across environments.
  • Version Constraints: Specify package versions cautiously to prevent automatic updates that might break your code.
  • Security: Regularly run npm audit to identify and fix known vulnerabilities in installed packages.

Conclusion

npm significantly simplifies the management of Node.js project dependencies. By mastering npm's commands and best practices, developers can enhance their productivity and maintain the health and security of their projects.

For further guidance on using npm, refer to the npm official documentation.