AskHandle

AskHandle Blog

How to Fix Common SonarQube Code Smells in Node.js Projects?

December 8, 2025Annie Hayes3 min read

How to Fix Common SonarQube Code Smells in Node.js Projects?

Code quality issues in Node.js projects can be challenging to resolve, especially when using SonarQube as your code analysis tool. This article explains practical solutions for fixing frequent code smells that SonarQube detects in Node.js applications.

What are Code Smells?

Code smells are patterns in code that suggest potential problems or areas for improvement. They don't necessarily indicate bugs but point to weaknesses in design that might cause issues later. SonarQube identifies these patterns and assigns them severity levels to help developers make informed decisions about what to fix first.

Common Code Smells and Solutions

1. Unused Variables and Imports

One of the most frequent issues SonarQube reports is unused variables and imports. These take up space and make code harder to read. To fix this:

javascript
1// Bad
2const fs = require('fs');
3const path = require('path');  // Never used
4const user = getUserData();    // Never used
5
6// Good
7const fs = require('fs');
8const userData = getUserData();
9processUserData(userData);

Use IDE features or npm packages like eslint to automatically detect and remove unused imports and variables.

2. Cognitive Complexity

When functions become too complex, SonarQube raises alerts. High cognitive complexity makes code hard to maintain. Here's how to reduce it:

javascript
1// Too complex
2function validateUser(user) {
3    if (user.age) {
4        if (user.age < 18) {
5            if (user.parentConsent) {
6                return true;
7            } else {
8                return false;
9            }
10        } else {
11            return true;
12        }
13    }
14    return false;
15}
16
17// Better approach
18function validateUser(user) {
19    if (!user.age) return false;
20    if (user.age >= 18) return true;
21    return Boolean(user.parentConsent);
22}

3. Magic Numbers

SonarQube often flags magic numbers in code. These are numerical values used directly without explanation:

javascript
1// Bad
2if (status === 200) {
3    processSuccess();
4}
5
6// Good
7const HTTP_STATUS_OK = 200;
8if (status === HTTP_STATUS_OK) {
9    processSuccess();
10}

Setting Up SonarQube Rules

To make the most of SonarQube analysis, configure your rules properly:

  1. Create a sonar-project.properties file in your project root:
properties
1sonar.projectKey=my-node-project
2sonar.sources=src
3sonar.exclusions=**/*.test.js,**/*.spec.js
4sonar.javascript.lcov.reportPaths=coverage/lcov.info
  1. Add specific rules to your .eslintrc:
json
1{
2    "extends": "sonarjs/recommended",
3    "rules": {
4        "sonarjs/no-duplicate-string": "error",
5        "sonarjs/cognitive-complexity": ["error", 5]
6    }
7}

Best Practices for Code Quality

Following these practices helps prevent code smells:

  1. Write smaller functions that do one thing well
  2. Use meaningful variable and function names
  3. Add comments for complex logic
  4. Implement consistent error handling
  5. Remove commented-out code

Regular Code Reviews

Set up regular code reviews using SonarQube's pull request analysis feature. This helps catch issues early:

yaml
1# In your CI pipeline
2stages:
3  - sonarqube-analysis
4
5sonarqube-check:
6  stage: sonarqube-analysis
7  script:
8    - sonar-scanner
9  only:
10    - merge_requests

Monitoring Progress

Track your code quality improvements over time using SonarQube's dashboard. Focus on:

  1. Maintainability rating
  2. Technical debt
  3. Code coverage
  4. Duplicated lines
  5. Number of code smells

Take small steps to improve these metrics. Fix the most critical issues first, then move to minor ones. This approach makes the process more manageable and shows steady progress.

Code quality is an ongoing process. Using SonarQube effectively with Node.js projects requires regular attention and consistent effort. Start with the most severe issues, establish good coding practices, and gradually work toward better