Sub topic 3
Installing the plugin
This chapter installs the Code Analyzer plugin for the Salesforce CLI and confirms it works. It also covers the failures people hit most often, which usually come from missing runtimes and not from the plugin.
Prerequisites
This chapter assumes you have the following:
- A terminal and the Salesforce CLI (
sf) already installed. - Permission to install global npm packages, or a Node.js setup that doesn't need administrator rights.
- A Salesforce DX project with a
force-appfolder. You don't need it for installation, but the verification steps use it later.
What gets installed
Code Analyzer is a plugin for sf. Installing it adds the sf code-analyzer command group. The plugin doesn't contain every analysis tool itself. It installs the engine packages it needs, and some of those engines run on other runtimes on your machine.
| Piece | Needed for | Notes |
|---|---|---|
| Salesforce CLI (`sf`) | Running any command | Installed from npm or the standalone installer |
| Node.js 20 or later | The CLI, the plugin, ESLint, RetireJS, Regex | Check the current minimum in the Salesforce documentation |
| Java 11 or later | PMD, CPD, and the graph engine | Must be reachable from your `PATH` |
| Python 3.10 or later | The Flow engine | Only needed if you run Flow rules |
Engines are covered in the previous chapter. Here the point is practical: a missing runtime disables only the engines that depend on it. The rest of the plugin keeps working.
Step 1: Check your runtimes
Check the runtimes before you install, because it is quicker than debugging afterward.
node --version
java -version
python3 --version
sf --versionYou need node and sf for sure. If java -version reports "command not found", PMD rules won't be available. PMD supplies most Apex rules, so this is the gap that affects you most. Python is only required when you want Flow rules.
Step 2: Install the CLI and the plugin
If sf is not installed yet, install it with npm. Then install the plugin and list the Recommended rules. This is the same sequence used throughout the tutorial.
npm install --global @salesforce/cli
sf plugins install code-analyzer
sf code-analyzer rules --rule-selector RecommendedThe third command is your smoke test. If a table of rules prints, the plugin loaded and at least some engines started.
If you installed sf with the standalone installer instead of npm, skip the first line.
Step 3: Verify the installation
Run these commands to confirm what you have.
sf plugins
sf code-analyzer --help
sf code-analyzer rules --rule-selector Recommended --view tablesf plugins lists user-installed plugins, and code-analyzer should appear with a version number. Note the version. When you read documentation or ask for help, the major version matters, because version 5 behaves differently from the older sf scanner plugin (version 4).
The rules command doesn't scan any code, so it's safe to run anywhere, including outside a Salesforce project.
Reading the first output
Rules appear grouped by name, engine, severity, and tags. A trimmed example looks like this:
# Name Engine Severity Tags
1 ApexCRUDViolation pmd 3 Recommended, Security, Apex
2 OperationWithLimitsInLoop pmd 3 Recommended, Performance, Apex
3 no-undef eslint 3 Recommended, JavaScriptThe exact rule names and counts change between releases, so don't expect your output to match this sample. What you should see is rules from more than one engine. If every row says pmd, or no row does, an engine failed to start. Look for warnings printed above the table.
Updating and removing
Update the plugin the way you update any sf plugin.
sf plugins updateTo remove it:
sf plugins uninstall code-analyzerIn CI or in a team repository, decide whether to pin a version. Without pinning, a pipeline picks up new rules when they ship, and a build that passed yesterday can fail today. Pinning gives you stable results, at the cost of updating on purpose. A reasonable approach is to float the version locally and pin it in CI, then update the pin in a reviewed change.
sf plugins install [email protected]Replace 5.0.0 with a version that exists. List the available versions with npm view @salesforce/plugin-code-analyzer versions.
Common errors and fixes
Permission denied on global install
npm install --global can fail with EACCES when npm's global folder is owned by root. Don't fix this with sudo. Either configure npm to use a prefix inside your home directory, or manage Node with a version manager such as nvm. Both avoid root-owned files that cause trouble later.
Java not found, or the wrong Java
PMD and the graph engine start a Java process. If java isn't on your PATH, those engines fail. If several Java versions are installed, the first one on PATH is used, and it may be older than 11. Fix your PATH, or point Code Analyzer at a specific Java in code-analyzer.yml.
# code-analyzer.yml
engines:
pmd:
java_command: /usr/lib/jvm/java-17/bin/javaYou'll meet this file properly in a later chapter. For now, treat it as the escape hatch when PATH can't be changed, such as on a locked-down build agent.
Node version too old
Old Node versions can produce syntax errors or engine startup failures that look unrelated to the version. Run node --version first when a message mentions an unexpected token or an unsupported API.
Plugin appears twice, or old commands remain
Version 4 (@salesforce/sfdx-scanner, commands under sf scanner) can be installed beside version 5. Having both is allowed, but it is confusing, because the two use different options and rule names. If you have no reason to keep v4, remove it.
sf plugins uninstall @salesforce/sfdx-scannerProxy or certificate failures
Corporate networks often block npm registry traffic or re-sign TLS connections. Failures show up as timeouts or certificate errors during sf plugins install. Set the proxy values in your npm configuration, or ask your network team for the registry address to allow. Don't turn off TLS verification to get around it.
Installing in a clean environment
On CI agents and fresh containers, nothing is preinstalled, so the install steps must be explicit. The order matters: Node first, then the CLI, then the plugin. Java and Python must also be present before the first scan, not just before installation. Many hosted runners ship with Java, but check your image instead of assuming. The CI chapter returns to this with a full workflow.
Exercise
You install the plugin, and sf code-analyzer rules --rule-selector Recommended prints only ESLint and Regex rules, with no PMD rules. Which prerequisite is the most likely cause, and how would you confirm it?
Show answer
Java 11 or later is probably missing or not on your PATH, because PMD needs it. Confirm by running java -version, then read the warnings printed above the rules table. Fix it by installing a suitable Java and fixing PATH, or by setting engines.pmd.java_command in code-analyzer.yml.
Exercise
Why might you pin the plugin version in CI but not on your laptop?
Show answer
A new plugin release can add or change rules, so an unpinned CI job can start failing without any code change. Pinning keeps results repeatable. Locally, the newest rules help you find issues early. The pin is then updated deliberately in a reviewed change.