How to Install Salesforce CLI with NPM

Introduction

Salesforce CLI has become essential for modern Salesforce development, enabling source-driven workflows and CI/CD pipelines. While there are multiple ways to install the CLI, this guide focuses on using Node Package Manager (NPM) – a method that aligns with modern software development practices, offering simplicity, flexibility, and robustness.

Note: The older sfdx-cli package is deprecated. This guide covers the current @salesforce/cli package. Both sf and sfdx commands work identically in the new CLI – your existing scripts remain compatible.

Having installation issues? See our guide on How to Fix Salesforce CLI NPM Installation Errors for solutions to common problems like permission errors and command not found.

Why NPM?

NPM, integral to the Node.js ecosystem, brings numerous benefits to Salesforce CLI installation:

  • Version Management: Easily switch between CLI versions to meet project-specific requirements. For example, npm install @salesforce/[email protected] --global for a specific version.
  • Version Pinning for CI/CD: Lock your team to a specific CLI version for reproducible builds.
  • Ecosystem Compatibility: Seamlessly integrates with other Node.js tools, enhancing tooling and automation capabilities in Salesforce development.
  • Cross-Platform Consistency: Provides a consistent installation experience across different operating systems, reducing setup overhead.

Prerequisites

  • Ensure you have Node.js and NPM installed (versions 20.9 or higher recommended). Visit Node.js for download and installation instructions.
  • Familiarity with basic command-line operations and NPM is assumed.

You can check your Node.js and NPM versions by running the commands node --version and npm --version respectively.

Detailed Installation Steps

  1. Open your terminal: Use Terminal on macOS/Linux, or PowerShell on Windows.
  2. Install Salesforce CLI: Run the following command to install the CLI globally:
    npm install @salesforce/cli --global
  3. Verify the installation: Check that the CLI is installed correctly:
    sf --version
    You should see output like @salesforce/cli/2.x.x. Both sf and sfdx commands work.
  4. Keep the CLI updated: Salesforce releases weekly updates with new features and security patches:
    npm update @salesforce/cli --global

Getting errors? Permission denied, command not found, or PowerShell policy errors are common. See How to Fix Salesforce CLI NPM Installation Errors for solutions.

Notes and Best Practices

  • Use nvm (Recommended): Installing Node.js via nvm (Node Version Manager) avoids permission issues and makes switching Node versions simple. This is the approach Salesforce recommends.
  • Local Installation for CI/CD: For reproducible builds, install a specific version locally in your project:
    npm install @salesforce/[email protected] --save-dev
    Then run commands via npx sf instead of the global sf.
  • sf vs sfdx Commands: Both commands work identically. The sfdx command is an alias for backward compatibility. Salesforce recommends using sf for new projects.
  • Integration with CI/CD: Salesforce CLI integrates with Jenkins, GitHub Actions, GitLab CI, and other automation tools. See the Salesforce CLI Setup Guide for details.

Conclusion

Using NPM for Salesforce CLI installation provides version control, CI/CD compatibility, and cross-platform consistency that standalone installers lack. Remember to use @salesforce/cli (not the deprecated sfdx-cli), and consider using nvm to avoid permission headaches.

If you run into installation problems, check out How to Fix Salesforce CLI NPM Installation Errors for step-by-step solutions to common issues.