Using the scanner
To start the SonarScanner for NPM, you can either add the analysis to your build files or use the scanner start command line (with or without npx).
You can start the scanner and thus, integrate it into your CI or build pipeline, in the following ways:
From the command line. A global mode installation of the scanner is required.
From the command line with npx. No scanner installation is required.
By adding the analysis step to your build files. The scanner must be added to the project’s devDependencies.
SonarScanner can be resource-intensive and may experience conflicts or reduced performance in environments protected by End-Point Detection and Response (EDR) software or other real-time antivirus tools. If you encounter issues, refine your security product configuration rather than disabling protection entirely. Depending on your security policy, this may include using detection-only mode or applying narrow, directory-level exclusions for scanner working directories. Any exclusions should be kept to the minimum necessary scope and reviewed periodically.
You can pass analysis parameters in the command line and in the analysis step coded in JS. In addition, the SonarScanner for NPM gets analysis parameters from different other sources: see Configuring the scanner. To get started, you must configure at a minimum the SonarQube Server URL and the token used to connect to the server.
Starting the scanner from the command line
Make sure the scanner is installed in global mode: see Installing the scanner.
Use the
sonar-scanner-npmcommand to start the analysis. To pass analysis parameters in the command line, use the standard-Dsonar.xxx=yyysyntax. Example:
sonar-scanner-npm -Dsonar.host.url=https://myserver.com -Dsonar.token=019d1e2e04ePassing a project key is optional: the scanner for NPM uses the name field of the package.json file as project key. However, you can override the project key by passing the -Dsonar.projectKey to the command line.
Starting the scanner from the command line with npx
Use the
npx @sonar/scancommand to start the analysis. To pass analysis parameters in the command line, use the standard-Dsonar.xxx=yyysyntax. Example:
npx @sonar/scan -Dsonar.host.url=https://myserver.com -Dsonar.token=019d1e2e04eAdding the analysis step to your build files
Make sure the scanner is installed in your project’s devDependencies: see Installing the scanner.
Code the analysis step in JS in your build files, as shown in the example below.
Where the syntax is as follows:
parameters (format: Map)
serverUrl (format: String; optional): The URL of the SonarQube Server instance. Defaults to the value of the SonarQube Cloud URL (
sonar.scanner.cloudUrlproperty).token (format: String; optional): The authentication token used to connect to your instance of SonarQube Server or SonarQube Cloud. Empty by default. See Managing your tokens for more information on tokens.
options (format: Map; optional): Used to pass extra parameters for the analysis. See Configuring the scanner for more details.
Starting the scanner from the command line with pnpx
To run the scanner with pnpm, use the following syntax:
Last updated
Was this helpful?

