Logo

dev-resources.site

for different kinds of informations.

8 essentials for every JavaScript project

Published at
11/18/2024
Categories
javascript
bestpractices
codequality
beginners
Author
vivianedias
Author
11 person written this
vivianedias
open
8 essentials for every JavaScript project

As a developer, especially if you’re new to a team, one of the fastest ways to add value is by introducing tools that improve the day-to-day workflow. These tools help maintain code quality, ensure consistency, and streamline the development process. Here’s a list of what I consider essentials for any JavaScript project:


1. Make code formatting consistent

  • Tool: Prettier Consistent code formatting reduces the "nitpicking" during code reviews and allows developers to focus on functionality. Prettier automatically formats your code based on defined rules.

Basic setup:

npm install --save-dev prettier
Enter fullscreen mode Exit fullscreen mode

Add a .prettierrc configuration file for your rules:

{
  "semi": true,
  "singleQuote": false
}
Enter fullscreen mode Exit fullscreen mode

Add a formatting script in your package.json:

"scripts": {
  "format": "prettier --write ."
}
Enter fullscreen mode Exit fullscreen mode

2. Enforce best practices

  • Tool: eslint ESLint ensures your code adheres to best practices and project-specific conventions. With plugins, you can tailor it to your framework and project requirements.

Basic setup:

npm install --save-dev eslint
Enter fullscreen mode Exit fullscreen mode

Initialize ESLint:

npx eslint --init
Enter fullscreen mode Exit fullscreen mode

Install framework-specific plugins (e.g., Next.js):

npm install --save-dev eslint-config-next
Enter fullscreen mode Exit fullscreen mode

Create a .eslintrc file for configuration or use the wizard setup.


3. Quick feedback on your changes

  • Tools: Husky + lint-staged Run linting and tests before committing or pushing code. This ensures only high-quality code is pushed to the repository.

Setup:

Install Husky and lint-staged:

npm install --save-dev husky lint-staged
Enter fullscreen mode Exit fullscreen mode

Enable Husky hooks:

npx husky install
Enter fullscreen mode Exit fullscreen mode

Add pre-commit and pre-push hooks:

npx husky add .husky/pre-commit "npx lint-staged"
npx husky add .husky/pre-push "npm run build"
Enter fullscreen mode Exit fullscreen mode

Configure lint-staged in package.json:

"lint-staged": {
  "*.js": ["eslint --fix", "prettier --write", "jest --findRelatedTests"]
}
Enter fullscreen mode Exit fullscreen mode

4. Pull-request static code analysis

  • Tool: SonarCloud Automates the detection of code smells, vulnerabilities, and potential bugs. Great for identifying issues early.

Setup:

Integrate SonarCloud into your CI pipeline using their documentation.

Add a sonar-project.properties file to configure the scanner.


5. Continuous integration (CI) pipeline

Setup example with GitHub Actions:

Create a .github/workflows/ci.yml file:

name: CI
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
        timeout-minutes: 5
        strategy:
      matrix:
        node-version: [20.x]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - name: Run build
        run: npm run build

      - name: Run unit tests
        run: npm run test
Enter fullscreen mode Exit fullscreen mode

6. Continuous Deployment (CD) Pipeline

  • Deploy to staging and production automatically using tools like GitHub Actions or other CI/CD services. Testing in staging ensures environment variables and integrations work before going live.

Setup example for staging and production deployments:

Add a job to your CI pipeline to deploy after tests pass:

deployment:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      matrix:
        node-version: [20.x]
    environment:
      name: ${{ github.ref == 'refs/heads/main' && 'production' || 'staging' }}
    needs:
      - integration
    if: always() && needs.integration.result == 'success'
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Install serverless globally
        run: npm install [email protected] --global

      - name: Serverless AWS authentication
        run: sls config credentials --provider aws --key ${{ secrets.AWS_ACCESS_KEY_ID }} --secret ${{ secrets.AWS_SECRET_ACCESS_KEY }}

      - name: Deploy API
        run: serverless deploy --stage $STAGE
Enter fullscreen mode Exit fullscreen mode

7. End-to-End testing

  • Tools: Cypress, Playwright E2E tests ensure your application works as expected in a browser.

Setup example with Cypress:

Install Cypress:

npm install --save-dev cypress
Enter fullscreen mode Exit fullscreen mode

Add a test script in package.json:

"scripts": {
  "test:e2e": "cypress open"
}
Enter fullscreen mode Exit fullscreen mode

8. Use TypeScript for type safety and documentation

  • Tool: TypeScript TypeScript adds static typing to JavaScript, catching errors at compile time and improving code readability and maintainability.

Setup:

Install TypeScript:

npm install --save-dev typescript
Enter fullscreen mode Exit fullscreen mode

Initialize a tsconfig.json file:

npx tsc --init
Enter fullscreen mode Exit fullscreen mode

Update your scripts in package.json:

"scripts": {
  "build": "tsc"
}
Enter fullscreen mode Exit fullscreen mode

Refactor your .js files to .ts and start enjoying type safety!


Adding these tools will significantly boost your project's maintainability and help your team focus on what matters most: building great features.

bestpractices Article's
30 articles in total
Favicon
Why Test Driven Development
Favicon
Go Serialization Essentials: Struct Tags, Error Handling, and Real-World Use Cases
Favicon
Creating Safe Custom Types with Validation in Go
Favicon
Best Practices for Network Management in Docker 👩‍💻
Favicon
Enforce DevOps best practices and eliminate production errors!
Favicon
The State of Cybersecurity Marketing: A Deep Dive Analysis
Favicon
Responsive Images: Best Practices in 2025
Favicon
Code Speaks for Itself: Métodos Bem Escritos Dispensam Comentários
Favicon
Generate 6 or 8 digit alpha numeric code using best performance in C#
Favicon
How To Replace Exceptions with Result Pattern in .NET
Favicon
8 essentials for every JavaScript project
Favicon
Send a From Header When You Crawl
Favicon
The Open Source AI : Understanding the New Standard
Favicon
Best Practices for Using Azure ATP in Hybrid Environments
Favicon
TADOConnection: Proper Use of LoginPrompt
Favicon
Best Practices for Developing and Integrating REST APIs into Web Applications
Favicon
Mastering Cybersecurity: A Comprehensive Guide to Self-Learning
Favicon
Best Practices for Data Security in Big Data Projects
Favicon
Hopefully Helpful Notes, Best Practices, ...
Favicon
Best Practices for Using GROUP BY in MySQL for Converting Vertical Data to JSON
Favicon
Best Practices in Software Architecture for Scalable, Secure, and Maintainable Systems
Favicon
Cloud Computing Security Best Practices for Enterprises
Favicon
Why Interfaces Are Essential in .NET Development
Favicon
Git Tricks You Should Know: Aliases, Bisect, and Hooks for Better Workflow
Favicon
Why pinning your dependency versions matters
Favicon
Microservices Best Practices: Multi-Tenant microservices with Java SDK
Favicon
Apple Intelligence: Pioneering AI Privacy in the Tech Industry
Favicon
Improving JavaScript Performance: Techniques and Best Practices
Favicon
Test-Driven Development (TDD) in Ruby: A Step-by-Step Guide
Favicon
APIs and Security Best Practices: JavaScript and Python Examples

Featured ones: