SonarQube/SonarCloud Scanner for JavaScript Projects
The `sonarqube-scanner` package provides a Node.js-based client to initiate code analysis on SonarQube Server and SonarCloud for JavaScript, TypeScript, and other compatible projects. Unlike the Java-based SonarScanner CLI, this module does not require a Java Runtime Environment, streamlining integration into JavaScript-centric build pipelines. The current stable version is 4.3.6, with frequent releases primarily focused on dependency updates, minor bug fixes, and security patches. It offers both a command-line interface (via `npx @sonar/scan` or global installation) and a programmatic API for integration into Node.js applications. Key differentiators include its lightweight nature for Node.js environments and direct support for `sonar.projectKey`, `sonar.sources`, and other SonarQube analysis properties.
Common errors
-
Error: Node.js version is not supported.
cause Running `sonarqube-scanner` (version 4.x or higher) with a Node.js version older than 18.fixUpgrade your Node.js environment to version 18 or newer (e.g., `nvm install 18 && nvm use 18`). -
Error: SonarQube server not found at URL: [YOUR_URL]
cause The configured SonarQube server URL is incorrect, inaccessible, or the server is not running.fixVerify the `serverUrl` option in your scanner configuration. Ensure the SonarQube server is running and accessible from the machine initiating the scan. Check firewall rules or proxy settings. -
Error: You must install 'sonarqube-scanner' globally to run the 'sonar' command.
cause Attempting to use the `sonar` command without installing the package globally.fixInstall the package globally using `npm install -g sonarqube-scanner` or use `npx sonarqube-scanner` instead to run it without global installation.
Warnings
- breaking Node.js version requirements have increased. Versions 4.x and above require Node.js 18+, while v3.x required Node.js 16+. Older versions (v2.9.1 and below) supported Node.js 14+.
- breaking The package `sonarqube-scanner` is officially published under this name. However, the project's README and quickstart examples frequently refer to `@sonar/scan` for installation and `npx` commands. This creates confusion regarding the correct package identifier. Always use `sonarqube-scanner` when installing or importing programmatically unless official documentation explicitly clarifies a renaming.
- gotcha Security vulnerabilities CVE-2024-21538 and CVE-2025-27152 (related to Axios) were fixed in version 4.3.0. Running older versions exposes projects to these known vulnerabilities.
- gotcha The `scanner` function's callback is executed upon the completion of the local scanning process, not necessarily after the SonarQube server has finished processing the analysis report. To get the actual quality gate status or detailed analysis results, you typically need to poll the SonarQube server API.
Install
-
npm install sonarqube-scanner -
yarn add sonarqube-scanner -
pnpm add sonarqube-scanner
Imports
- default
const scanner = require('sonarqube-scanner').default;import scanner from 'sonarqube-scanner'; // or import * as scanner from 'sonarqube-scanner';
- scanner function
import { scanner } from 'sonarqube-scanner';import scanner from 'sonarqube-scanner'; scanner({ /* options */ }, callback); - Command Line Interface (CLI)
npx sonarqube-scanner # or globally installed sonar
Quickstart
import scanner from 'sonarqube-scanner';
import path from 'node:path';
const projectKey = process.env.SONAR_PROJECT_KEY ?? 'my-typescript-project';
const serverUrl = process.env.SONAR_SERVER_URL ?? 'http://localhost:9000';
const token = process.env.SONAR_TOKEN ?? ''; // Optional, for authenticated scans
console.log(`Starting SonarQube scan for project: ${projectKey}`);
scanner(
{
serverUrl: serverUrl,
token: token, // Pass token only if required for authentication
options: {
'sonar.projectKey': projectKey,
'sonar.projectName': projectKey, // Often same as key
'sonar.projectVersion': '1.0.0',
'sonar.sources': 'src',
'sonar.tests': 'test',
'sonar.typescript.lcov.reportPaths': 'coverage/lcov.info',
'sonar.javascript.maxFileSize': '10000',
'sonar.sourceEncoding': 'UTF-8',
'sonar.exclusions': '**/node_modules/**, **/*.d.ts',
'sonar.host.url': serverUrl, // Redundant if serverUrl is set directly, but common
// Add other properties as needed, e.g., 'sonar.login': token for older versions
},
},
() => {
console.log('SonarQube scan finished or failed (check SonarQube logs).');
// The callback is invoked regardless of success/failure,
// so check SonarQube server for actual status.
}
);