SonarQube/SonarCloud Scanner for JavaScript Projects

4.3.6 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This TypeScript example demonstrates how to programmatically trigger a SonarQube analysis for a project, configuring common properties like project key, sources, tests, and coverage reports. It uses environment variables for sensitive data.

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.
  }
);

view raw JSON →