52. How do you install packages locally and globally using npm?

npm allows installing packages both locally (for a specific project) and globally (for the entire system).

Local installation:

  • Command:
npm install <package-name>
  • Description: Packages are installed in the project's node_modules directory and are only available for that project. Dependencies are recorded in the package.json file under dependencies.

Example:

npm install express

Global installation:

  • Command:
npm install -g <package-name>
  • Description: Packages are installed globally and are available from anywhere in the system. Global installation is mainly used for command-line tools like webpack, gulp, etc.

Example:

npm install -g webpack

Managing local and global packages:

  • List locally installed packages:
npm list
  • List globally installed packages:
npm list -g --depth=0
  • Uninstall a package locally:
npm uninstall <package-name>
  • Uninstall a package globally:
npm uninstall -g <package-name>

Installing packages locally and globally using npm allows for flexible management of project dependencies and development tools.

devFlipCards 2024

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.