> For the complete documentation index, see [llms.txt](https://docs.sonarsource.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sonarsource.com/sonarqube-server/10.8/analyzing-source-code/scanners/npm/using.md).

# 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). This page is for SonarQube Server 10.8.

{% hint style="info" %}
**For AI agents:** You're in the SonarQube Server 10.8 documentation. For a list of all 10.8 pages, see the [SonarQube Server 10.8 page index](https://312504542-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiJj3TXBdWssTGGg8qK5I%2Fuploads%2FmeJ1nVNGPezOurNrTy4A%2Fsonarqube-server-10.8-llms.txt?alt=media). For the whole site, see the [page index](https://docs.sonarsource.com/llms.txt) or the [full text](https://docs.sonarsource.com/llms-full.txt). For another version, replace 10.8 in the URL with that version. The [latest version](https://docs.sonarsource.com/sonarqube-server/) has no version number in its URL.
{% endhint %}

You can start the [Introduction](/sonarqube-server/10.8/analyzing-source-code/scanners/npm/introduction.md) 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.

{% hint style="warning" %}
We do not recommend running an antivirus scanner on the machine where a SonarQube Server analysis runs, it could result in unpredictable behavior.
{% endhint %}

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](/sonarqube-server/10.8/analyzing-source-code/scanners/npm/configuring.md). To get started, you must configure at a minimum the [Analysis parameters](/sonarqube-server/10.8/analyzing-source-code/analysis-parameters.md) and the [Analysis parameters](/sonarqube-server/10.8/analyzing-source-code/analysis-parameters.md) used to connect to the server.

{% hint style="info" %}
The SonarScanners run on code that is checked out. See [Checked-out code](/sonarqube-server/10.8/analyzing-source-code/scanners/scanner-environment/verifying-code-checkout-step.md).
{% endhint %}

## Starting the scanner from the command line <a href="#command-line" id="command-line"></a>

1. Make sure the scanner is installed in global mode: see [Installing the scanner](/sonarqube-server/10.8/analyzing-source-code/scanners/npm/installing.md).
2. Use the `sonar` command to start the analysis.\
   To pass analysis parameters in the command line, use the standard `-Dsonar.xxx=yyy` syntax.\
   Example:

```css-79elbk
sonar -Dsonar.host.url=https://myserver.com -Dsonar.token=019d1e2e04e
```

## Starting the scanner from the command line with npx <a href="#npx" id="npx"></a>

* Use the `npx @sonar/scan` command to start the analysis.\
  To pass analysis parameters in the command line, use the standard `-Dsonar.xxx=yyy` syntax.\
  Example:

```css-79elbk
npx @sonar/scan -Dsonar.host.url=https://myserver.com -Dsonar.token=019d1e2e04e
```

## Adding the analysis step to your build files <a href="#add-to-build-files" id="add-to-build-files"></a>

1. Make sure the scanner is installed in your project’s devDependencies: see [Installing the scanner](/sonarqube-server/10.8/analyzing-source-code/scanners/npm/installing.md).
2. Code the analysis step in JS in your build files, as shown in the example below.

```css-79elbk
const scanner = require('@sonar/scan');
scanner(
  {
    serverUrl: 'https://sonarqube.mycompany.com',
    token: '019d1e2e04eefdcd0caee1468f39a45e69d33d3f', 
    options: {
      'sonar.projectName': 'My App',
      'sonar.projectDescription': 'Description for "My App" project...',
      'sonar.sources': 'src',
      'sonar.tests': 'test', 
    },
  },
  () => process.exit(),
);
```

Where the syntax is as follows:

```css-79elbk
scanner ( parameters, [callback] )
```

* 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.cloudUrl` property).
  * token (format: String; optional): The [Managing your tokens](/sonarqube-server/10.8/user-guide/managing-tokens.md) used to connect to your instance of SonarQube Server or SonarQube Cloud. Empty by default.
  * options (format: Map; optional): Used to pass extra parameters for the analysis. See [Configuring the scanner](/sonarqube-server/10.8/analyzing-source-code/scanners/npm/configuring.md) for more details.
* callback (format: Function; optional): Callback (the execution of the analysis is asynchronous).

## Starting the scanner from the command line with pnpx <a href="#pnpx" id="pnpx"></a>

`@sonar/scan` has multiple binaries, so pnpx will ask which binary to provide. The approach recommended by pnpm is to use the following syntax:

```css-79elbk
pnpm --package=@sonar/scan dlx sonar -Dsonar.host.url=https://myserver.com -Dsonar.token=019d1e2e04e
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sonarsource.com/sonarqube-server/10.8/analyzing-source-code/scanners/npm/using.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
