Node.js CI Workflow with npm

Create a GitHub Actions CI workflow for Node.js projects. Covers checkout, setup-node, npm install, lint, test, and build steps with caching.

Basic CI

Detailed Explanation

Building a Node.js CI Pipeline

A Node.js CI workflow is the foundation of quality assurance for any JavaScript or TypeScript project. It ensures that every push and pull request passes linting, testing, and build checks before code is merged.

Workflow YAML

name: Node.js CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run linter
        run: npm run lint

      - name: Run tests
        run: npm test

      - name: Build
        run: npm run build

Key Concepts

  • npm ci vs npm install: npm ci is faster in CI because it installs exactly what is in package-lock.json without modifying it. It also deletes node_modules/ first for a clean install.
  • Built-in caching: The actions/setup-node@v4 action supports a cache input that automatically caches and restores the npm/yarn/pnpm cache directory, significantly speeding up installs.
  • Branch filtering: Triggering only on main prevents CI runs on feature branches that may not be ready for review, though many teams choose to run CI on all branches.

Extending the Workflow

Add a coverage step with npm run test -- --coverage and upload results using actions/upload-artifact. For monorepos, use path filtering to only run CI when relevant packages change.

Use Case

Standard CI pipeline for Node.js web applications, REST APIs, or npm packages. Ensures every commit passes lint, test, and build before merge, catching regressions early in the development cycle.

Try It — GitHub Actions Workflow Builder

Open full tool