Skip to main content

npm command

Here’s a list of commonly used npm commands, including those related to configuration. These commands will help you manage your Node.js projects, packages, and npm configurations effectively.

1. Basic npm Commands

  •   npm config ls -l
  • npm init: Initialize a new Node.js project and create a package.json file.

    npm init
    • Use npm init -y to automatically create a package.json with default values.
  • npm install (or npm i): Install all dependencies listed in package.json.

    npm install
    • npm install <package-name>: Install a specific package locally.
    • npm install -g <package-name>: Install a package globally.
  • npm uninstall <package-name>: Uninstall a package.

    npm uninstall <package-name>
    • Use -g to uninstall a global package.
  • npm update: Update all outdated packages to their latest versions.

    npm update
  • npm outdated: Check for outdated packages.

    npm outdated
  • npm list (or npm ls): List installed packages.

    npm list
    • Use -g to list globally installed packages.
    • Use --depth=0 to list only top-level packages.
  • npm run <script>: Run a script defined in the scripts section of package.json.

    npm run build
  • npm test: Run the test script defined in package.json.

    npm test
  • npm start: Run the start script defined in package.json.

    npm start
  • npm publish: Publish a package to the npm registry.

    npm publish
  • npm unpublish: Remove a package from the npm registry.

    npm unpublish <package-name>

2. npm Configuration Commands

  • npm config set <key> <value>: Set a configuration value.

    npm config set prefix ~/.npm-global
  • npm config get <key>: Get the value of a configuration setting.

    npm config get prefix
  • npm config delete <key>: Delete a configuration setting.

    npm config delete prefix
  • npm config list: List all npm configuration settings.

    npm config list
  • npm config edit: Edit the .npmrc configuration file directly in a text editor.

    npm config edit

3. Package Management and Information

  • npm info <package-name>: Show information about a package.

    npm info react
  • npm view <package-name>: View details about a package (similar to npm info).

    npm view lodash
  • npm search <terms>: Search for packages in the npm registry.

    npm search express
  • npm audit: Perform a security audit of your project’s dependencies.

    npm audit
  • npm audit fix: Automatically fix vulnerabilities found in the audit.

    npm audit fix
  • npm cache clean --force: Clear the npm cache.

    npm cache clean --force

4. Global npm Commands

  • npm root -g: Display the directory where global packages are installed.

    npm root -g
  • npm list -g --depth=0: List globally installed packages.

    npm list -g --depth=0
  • npm link: Symlink a package folder into the global package directory.

    npm link
    • Use npm link <package-name> to link a global package as a local dependency.
  • npm unlink <package-name>: Remove a symlink created by npm link.

    npm unlink <package-name>

5. Scripts and Automation

  • npm run <script>: Run a script defined in the scripts section of package.json.

    npm run build
  • npm init <initializer>: Initialize a project using a specific initializer.

    npm init react-app my-app
  • npx <command>: Run a command from a package that isn't installed globally.

    npx create-react-app my-app

6. Package Lock and Dependency Management

  • npm ci: Install dependencies from package-lock.json, ensuring a clean state.

    npm ci
  • npm shrinkwrap: Lock down the versions of a project's dependencies.

    npm shrinkwrap
  • npm dedupe: Reduce duplication in the node_modules directory.

    npm dedupe

7. Useful Options

  • npm --version (or npm -v): Check the npm version.

    npm --version
  • npm help <command>: Get help on a specific npm command.

    npm help install
  • npm init -y: Automatically generate a package.json file with default values.

    npm init -y

Summary

These commands should cover most of your needs when working with npm and Node.js. Whether you're managing dependencies, configuring your environment, or automating tasks, this list provides a solid foundation for efficient project management.