Node.js Project Makefile

Create a Makefile for Node.js projects wrapping npm scripts with install, build, test, lint, and development server targets for a consistent interface.

Language-Specific

Detailed Explanation

Why Use Make with Node.js?

Node.js projects typically use npm scripts in package.json, but a Makefile adds a consistent interface across polyglot projects, dependency tracking (only reinstall when package.json changes), and the ability to compose complex workflows.

Dependency Tracking

node_modules: package.json package-lock.json
	npm ci
	@touch node_modules

install: node_modules

This is where Make truly shines. The node_modules target depends on package.json and package-lock.json. Make only runs npm ci when either file changes, saving time on repeated builds. The @touch updates the directory's modification time for accurate tracking.

Build Pipeline

DIST_DIR = dist

build: node_modules
	npm run build

dev: node_modules
	npm run dev

test: node_modules
	npm test

lint: node_modules
	npm run lint

lint-fix: node_modules
	npm run lint -- --fix

Each target depends on node_modules, ensuring packages are installed before any operation. This eliminates the common "did you run npm install?" question.

Production Build

.PHONY: production
production: clean install build
	@echo "Production build complete"

The production target chains clean, install, and build in sequence, providing a single command for CI/CD pipelines.

Clean Target

clean:
	rm -rf $(DIST_DIR) node_modules .next .cache coverage

A thorough clean target removes build artifacts, installed packages, and cache directories.

Key Advantages

  • make build works the same regardless of whether the project uses npm, yarn, or pnpm
  • Dependencies between targets prevent running tests before installing packages
  • File-based dependency tracking avoids unnecessary npm install runs

Use Case

Providing a standard build interface for a Node.js monorepo or a team project where some developers are not familiar with npm scripts, or integrating Node.js builds into a larger multi-language build system.

Try It — Makefile Generator

Open full tool