WikiGalaxy

Personalize

Introduction to Node Package Manager (NPM)

What is NPM?

Node Package Manager (NPM) is a package manager for the JavaScript programming language, primarily used for managing libraries and dependencies for Node.js projects.

Why Use NPM?

NPM simplifies the process of installing, updating, and managing packages, allowing developers to easily incorporate third-party libraries into their projects.

Core Features of NPM

  • Package Installation
  • Version Management
  • Dependency Management

NPM Commands Overview

NPM provides a range of commands for package management, including npm install, npm update, and npm uninstall.

NPM and Node.js Ecosystem

NPM is integral to the Node.js ecosystem, providing access to a vast library of open-source packages that enhance Node.js applications.

Customizing NPM Behavior

Developers can customize NPM behavior through configuration files and command-line options to suit project-specific needs.

Installing Packages with NPM

Single Package Installation

Install a single package locally using the npm install <package-name> command.


        npm install express
        

Global Package Installation

Install a package globally to use it across multiple projects using the -g flag.


        npm install -g nodemon
        

Installing Specific Versions

Specify a version to install a particular version of a package.


        npm install lodash@4.17.21
        

Installing from Package.json

Install all dependencies listed in a project's package.json file.


        npm install
        

Installing Development Dependencies

Use the --save-dev flag to install packages needed only for development.


        npm install --save-dev jest
        

Managing Dependencies with NPM

Updating Packages

Use npm update to update packages to their latest versions.


        npm update express
        

Checking for Outdated Packages

Use npm outdated to list packages that are outdated and need updates.


        npm outdated
        

Removing Packages

Remove a package using npm uninstall.


        npm uninstall express
        

Listing Installed Packages

List all installed packages using npm list.


        npm list
        

Handling Peer Dependencies

Manage peer dependencies by checking compatibility with other packages.


        npm install --legacy-peer-deps
        

NPM Scripts

Defining Custom Scripts

Define custom scripts in package.json to automate tasks.


        {
          "scripts": {
            "start": "node app.js"
          }
        }
        

Running Scripts

Run scripts using npm run <script-name>.


        npm run start
        

Using Pre and Post Hooks

Automate tasks before or after a script runs using pre and post hooks.


        {
          "scripts": {
            "prestart": "echo 'About to start'",
            "start": "node app.js",
            "poststart": "echo 'Started'"
          }
        }
        

Concurrent Script Execution

Use packages like concurrently to run multiple scripts simultaneously.


        npm install concurrently --save-dev
        

Environment Variables in Scripts

Pass environment variables to scripts using cross-env.


        npm install cross-env --save-dev
        

Publishing Packages to NPM

Creating a New Package

Initialize a new package using npm init.


        npm init
        

Publishing a Package

Publish your package to the NPM registry using npm publish.


        npm publish
        

Updating a Published Package

Increment the version number in package.json and republish.


        npm version patch
        npm publish
        

Unpublishing a Package

Remove a package from the registry using npm unpublish.


        npm unpublish --force
        

Managing Access Control

Manage who can access or modify your package using npm access.


        npm access public <package-name>
        

NPM Configuration and Customization

Configuring NPM

Use npm config to customize NPM settings.


        npm config set init-author-name "Your Name"
        

Viewing Configuration Settings

View current configuration settings using npm config list.


        npm config list
        

Resetting Configuration

Reset a configuration setting to its default value.


        npm config delete init-author-name
        

Setting Up Proxies

Configure proxies for NPM using npm config.


        npm config set proxy http://proxy.company.com:8080
        

Using Environment Variables

Utilize environment variables to influence NPM behavior.


        export NPM_CONFIG_LOGLEVEL=verbose
        

Troubleshooting Common NPM Issues

Resolving Conflicts

Conflicts can arise from incompatible package versions. Use npm dedupe to resolve them.


        npm dedupe
        

Clearing Cache

Clear the NPM cache to fix corrupted or outdated cache issues.


        npm cache clean --force
        

Diagnosing Installation Errors

Use the --verbose flag to get detailed error logs.


        npm install --verbose
        

Rebuilding Packages

Rebuild packages to ensure they are correctly compiled for your system.


        npm rebuild
        

Fixing Permissions Issues

Resolve permission errors by using sudo or configuring NPM to use a different directory.


        sudo chown -R $USER /usr/local/lib/node_modules
        
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025