SonarQube Properties for Node JS Project [ 3 steps]

In this article, We are going to cover , How to add SonarQube Properties for Node JS Project/sonarqube nodejs code coverage, Run SonarQube Test for Node.js Project and GitLab CI/CD Setup for Node.js Project.

Introduction

SonarQube is an opensource web based tool to manage code quality and code analysis. It is most widely used in continuous code inspection which performs reviews of code to detect bugs, code smells and vulnerability issues of programming languages such as PHP, C#, JavaScript, C/C++ and Java.

Node.js is free and an open-source cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code. It is used in developing web and networking applications.

Prerequisite

  • Preinstalled SonarQube with access
  • Node.js project’s access

Step #1: Setup SonarQube

Please follow below articles if you not installed SonarQube

How to Install SonarQube on Ubuntu 20.04 LTS

How to Install SonarQube on Ubuntu 18.04/16.04 LTS

Step #2: SonarQube Properties for Node JS Project

Once you installed , configured SonarQube Server.

create a sonar-project.js file in your project’s root directory and paste the below lines

sudo nano sonar-project.js

paste the below lines

const sonarqubeScanner = require('sonarqube-scanner');

sonarqubeScanner({
  serverUrl: 'http://sonarqube.fosstechnix.info/',
       options : {
       'sonar.sources': '.',
       'sonar.inclusions' : 'src/**' 
       },
}, () => {});

now include sonar-project.js node.js project’s package.json file as shown below

{
 .....
 ....
 "sonar": "node sonar-project.js"
},

Step #3: Run SonarQube Test for Node.js Project

once you changed in package.json file. Now run the SonarQube for your node.js project using below command

npm run sonar

Step #4: GitLab CI/CD Setup for Node.js Project

If you are using GitLab CI/CD to deploy node.js project then add below stage in .gitlab-ci.yml file.

stages:
  - sonarqube-test

Add the below dependencies in .gitlab-ci.yml to install npm and run the sonarqube test for node.js application

sonarqube-nodejs:
  stage: sonarqube-test
  when: manual
  script: 
    - npm install
    - npm run sonar
  allow_failure: true

Go to CI/CD tab in GitLAB -> Click on Pipelines -> You can see sonarqube-nodejs in pipeline and click on Play button to run SonarQube test.

once pipeline ran successfully, open you sonarqube URL and you can see your node.js project with reports.

http://sonarqube.fosstechnix.info/projects

Conclusion

We have covered, How to add SonarQube Properties for Node JS Project, Run SonarQube Test for Node.js Project and GitLab CI/CD Setup for Node.js Project.

Related Articles

Angular CI CD Pipeline with GitLab in 6 Easy Steps

GitLab CI CD Pipeline for php,Python,Drupal Application[6 steps]

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link