Error when using scanner for npm

I get an error when executing sonar scanner for npm localy in vscode for a sonarqube cloud project:

sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.token=“my-sonar-token

[INFO] Bootstrapper: Retrieving info from “package.json” file
[INFO] Bootstrapper: Platform: win32 x64
[INFO] Bootstrapper: Server URL: https://sonarcloud.io
[INFO] Bootstrapper: Version: 4.3.0
[INFO] Bootstrapper: JRE provisioning is supported
[ERROR] Bootstrapper: An error occurred: AxiosError: Request failed with status code 401

I tried using a project token as well as a personal token. Same error.

Hi,

Welcome to the community!

Are you providing sonar.organization and sonar.projectKey to the analysis?

 
Thx,
Ann

Hi Ann,
thank you.
Initially I didnt. I tried now, but I get the same result.

Hi,

Can you remove any quotes from around your parameter values and try again?

 
Thx,
Ann

Hi Ann,
I didnt use any quotes with the parameter values, so nothing to remove :wink:
In the meantime I tried executing the scan in Visual Studio 2022 and it worked (sonar.organization and sonar.projectKey must provided).
So current status is: Using the same command in Visual Studio 2022 and VS Code. VS 2022 works, VS Code results in the 401 error.

Hi,

Are you using SonarQube for VSCode and getting the error from that? Or are you executing an analysis command directly from your IDE?

 
Ann

I am executing the command from the IDE.

Hi,

You’re pursing a use case we haven’t targeted. Analysis is meant to be run from a command line. The fact that you were able to get this working within Visual Studio (:face_with_raised_eyebrow:) suggests to me that the problem is specific to VSCode.

 
Ann

Hello, I’m having the same exact error when trying to use the scanner for npm.

executing from the command line:

sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.token=8bXXXXXXXXXX -Dsonar.projectKey=myprojectkey -Dsonar.organization=myorganization

I get:
[INFO] Bootstrapper: Retrieving info from “package.json” file
[INFO] Bootstrapper: Platform: win32 x64
[INFO] Bootstrapper: Server URL: https://sonarcloud.io
[INFO] Bootstrapper: Version: 4.3.0
[INFO] Bootstrapper: JRE provisioning is supported
[ERROR] Bootstrapper: An error occurred: AxiosError: Request failed with status code 401

Hi @Marco_Cordeiro,

Welcome to the community!

401 is ‘unauthorized’, so at a guess, the token isn’t valid for the project you’re trying to analyze.

If you generate a new token (from a user with analysis perms on the project) and it still fails, please create a new thread.

 
Thx,
Ann

Hello, even I’m facing the same issue despite creating a global token (Administration > Security > Users > Generate new token, the user is administrator with groups sonar-administrators and sonar-users)

I’m basically trying to run a JS file which is as follows:

const scanner = require('@sonar/scan').default;

const path = require('path');

require('dotenv').config();

require('dotenv').config({ path: path.resolve(process.cwd(), '.env.local') });

const pkg = require('../package.json');

const PATH_TO_LCOV_FILE =

'./coverage/ut/lcov.info'; /* coverage report path relative to root */




const SOURCE = 'src';

const GIT_HOST = 'Github';

const ENCODING = 'utf-8';

const EXCLUSION_PATTERNS = [

'**/__tests__/**',

'**/__mocks__/**',

'**/*.styled.*',

];


const ORG_NAME = "ORG_ABC";

scanner(

    {

serverUrl: process.env.SONAR_HOST_URL,

token: process.env.SONAR_TOKEN,

options: {

'sonar.host.url': process.env.SONAR_HOST_URL,

'sonar.token': process.env.SONAR_TOKEN,

'sonar.projectkey': pkg.name,

'sonar.organization': ORG_NAME,

'sonar.projectName': pkg.name,

'sonar.projectDescription': pkg.description,

'sonar.sources': SOURCE,

'sonar.tests': 'test',

'sonar.coverage.exclusions': EXCLUSION_PATTERNS.toString(),

'sonar.pullrequest.provider': GIT_HOST,

'sonar.pullrequest.github.repository': repoName,

'sonar.pullrequest.key':process.env.SONAR_CHANGE_ID /* pushed to env via pipeline */,

'sonar.pullrequest.branch': process.env.SONAR_CHANGE_BRANCH /* pushed to env via pipeline */,

'sonar.pullrequest.base':process.env.SONAR_CHANGE_TARGET /* pushed to env via pipeline */,

'sonar.branch.name':process.env.SONAR_BRANCH_NAME /* pushed to env via pipeline */,

        },

    },

    () => process.exit()

);

On running the script, this is what I’m seeing on terminal:

node sonar/sonar-scanner.js – source=local
[INFO] Bootstrapper: Retrieving info from “package.json” file
[INFO] Bootstrapper: Platform: darwin arm64
[INFO] Bootstrapper: Server URL: http://localhost:8282
[INFO] Bootstrapper: Version: 4.3.2
[INFO] Bootstrapper: SonarQube server version: 25.12.0
[INFO] Bootstrapper: JRE provisioning is supported
[ERROR] Bootstrapper: An error occurred: AxiosError: Request failed with status code 401

On further debugging the cause for 401, I’m seeing that it is trying to hit ‘GET /api/v2/analysis/jres?os=darwin&arch=arm64 HTTP/1.1\r\n’ which is giving 401

Hi @prajwalk,

Welcome to the community!

You’ve resurrected a topic that’s 6 months old. Per the FAQ, please don’t do that. Please create a new thread with all your details.

 
Thx,
Ann