As a React Native and MERN developer, I recently integrated SonarQube into my local workflow using Docker. Here's a straightforward guide to get you started.
What is SonarQube?
SonarQube automatically scans your code to find:
- Bugs and potential errors
- Security vulnerabilities
- Code smells (maintainability issues)
- Test coverage gaps
- Duplicate code
Prerequisites
Install Docker Desktop:
brew install --cask docker
Open Docker Desktop from Applications and make sure it's running.
Setup Steps (project👨💻)
1. Create Docker Compose File
Create docker-compose.yml in your project root:
services:
sonarqube:
image: sonarqube:community
container_name: sonarqube-local
ports:
- "9000:9000"
environment:
- SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
networks:
- sonarqube-network
volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
networks:
sonarqube-network:
driver: bridge
2. Add Scripts to package.json
{
"scripts": {
"sonar": "sonar-scanner",
"sonar:start": "docker-compose up -d",
"sonar:stop": "docker-compose stop",
"sonar:down": "docker-compose down",
"sonar:logs": "docker-compose logs -f sonarqube"
}
}
3. Create SonarQube Configuration
Create sonar-project.properties:
# Project identification
sonar.projectKey=my-app
sonar.projectName=My App
sonar.projectVersion=1.0.0
# Source code location
sonar.sources=src
sonar.tests=__tests__
# Exclude directories
sonar.exclusions=\
**/node_modules/**,\
**/android/**,\
**/ios/**,\
**/vendor/**,\
**/coverage/**,\
**/__mocks__/**,\
**/build/**,\
**/*.spec.ts,\
**/*.spec.tsx,\
**/*.test.ts,\
**/*.test.tsx,\
**/assets/**,\
**/*.config.js,\
**/*.config.ts,\
**/*.config.mjs,\
**/babel.config.js,\
**/metro.config.js,\
**/jest.config.js,\
**/jest-setup.ts
# Test exclusions
sonar.test.exclusions=\
**/node_modules/**,\
**/coverage/**
# Language
sonar.language=ts
# Encoding
sonar.sourceEncoding=UTF-8
# TypeScript specific
sonar.typescript.node=node
# Coverage report paths
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.coverage.exclusions=\
**/__tests__/**,\
**/__mocks__/**,\
**/*.test.ts,\
**/*.test.tsx,\
**/*.spec.ts,\
**/*.spec.tsx
# Disable external issues
sonar.typescript.internal.typescriptLocation=node_modules/typescript
# Host URL
sonar.host.url=http://localhost:9000
# Authentication token (add after generating)
# sonar.login=your-token-here
4. Update .gitignore
Add to your .gitignore:
# SonarQube
.scannerwork/
.sonarqube/
5. Start SonarQube
# Start Docker Desktop first
open -a Docker
# Start SonarQube server
yarn sonar:start
# Wait 1-2 minutes, then check logs
yarn sonar:logs
6. Generate Authentication Token
- Open http://localhost:9000
- Login with
admin/admin - Change password when prompted
- Go to Profile Icon → My Account → Security
- Generate token:
- Name:
local-scanner - Type:
Global Analysis Token
- Name:
- Copy the token immediately!
7. Add Token to Configuration
Edit sonar-project.properties and uncomment the last line:
sonar.login=your-generated-token-here
8. Install Sonar Scanner
npm install --save-dev sonar-scanner
# or
yarn add -D sonar-scanner
9. Run Your First Scan
# Run tests with coverage
yarn test --coverage
# Run SonarQube analysis
yarn sonar
View results at http://localhost:9000
Daily Usage
# Start server (if not running)
yarn sonar:start
# After making code changes
yarn test --coverage && yarn sonar
# View results at http://localhost:9000
# Stop server when done
yarn sonar:stop
Configuration for Different Projects
For Node.js/Express Backend:
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.test.js,**/*.spec.js
For React Native:
sonar.sources=src
sonar.tests=__tests__
sonar.exclusions=**/android/**,**/ios/**
Common Issues
Docker not running:
open -a Docker
# Wait 30 seconds, then retry
Port 9000 in use:
lsof -i :9000
# Kill the process or change port in docker-compose.yml
Authentication error:
# Verify token in sonar-project.properties
cat sonar-project.properties | grep sonar.login
What Gets Analyzed
- Code quality issues
- Potential bugs
- Security vulnerabilities
- Test coverage
- Duplicate code
- Complexity metrics
Key Benefits
✅ Catch bugs before production
✅ Improve code maintainability
✅ Track test coverage
✅ Enforce coding standards
✅ Identify security issues early
Useful Commands
yarn sonar:start # Start server
yarn sonar:stop # Stop server
yarn sonar:down # Stop and remove containers
yarn sonar:logs # View logs
yarn sonar # Run analysis
That's it! Keep SonarQube running in the background and scan your code regularly to maintain high-quality standards.
Resources:


Top comments (0)