LESSON 76 · ADVANCED4 min read

npm Scripts, Linting & Formatting

Turn project commands into a stable interface and automate consistency with linting, formatting, and reproducible installs.

ES2024 / Node.js 20+Free to readModule 17 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Define clear package scripts for development, testing, and building.
  • Separate ESLint's correctness checks from Prettier's formatting decisions.
  • Use lockfiles and semantic versioning to make installs reproducible and upgrades deliberate.

01Scripts are the project's control panel

Every repository should answer three questions with commands: how do I start it, how do I verify it, and how do I build it? Put those answers in package.json scripts. A new contributor then runs npm run dev, npm test, and npm run build without learning a private shell history. CI uses the same interface.

Keep scripts small and composable. One script can call another with npm run, and passing arguments requires a separator: npm run test -- --watch. Conventional names matter: start runs the production entry point, test runs tests, and build produces deployable output. Document anything unusual in the README.

package.jsonJSON
{  "name": "tasks-api",  "version": "1.0.0",  "type": "module",  "engines": { "node": ">=22" },  "scripts": {    "dev": "node --watch src/server.js",    "start": "node src/server.js",    "test": "node --test",    "lint": "eslint .",    "check": "npm run lint && npm test"  }}

02Development, build, and environment config

Development should be fast and forgiving; production should be reproducible and explicit. node --watch restarts on file changes without a third-party tool. A build step may bundle, transpile, or copy assets, but it should be deterministic: the same commit and lockfile produce the same artifact. Keep build output out of version control.

Configuration belongs in the environment, not in a branch. Read values once at startup, validate required ones, and fail with a clear message. Node can load a local file with node --env-file=.env, but that file stays out of git. NODE_ENV=production changes library behaviour, so set it in the deployed environment rather than relying on a developer's shell.

config.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03ESLint finds problems; Prettier ends arguments

ESLint analyses code for likely bugs and unwanted patterns: unused variables, accidental globals, unreachable code, and project-specific rules. Prettier formats code and does not try to find bugs. Running both is normal, but let Prettier own whitespace so ESLint rules about indentation do not fight it.

Use a flat eslint.config.js, enable the recommended rules, and add only rules your team will follow. A lint rule that everyone disables is noise. Run eslint . in CI and in the editor. Prettier's --check mode fails when files are unformatted; --write fixes them locally. Commit the configuration so every machine agrees.

eslint.config.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Editor feedback and pre-commit checks

An editor that formats on save and shows lint errors removes a whole class of review comments. Install the ESLint and Prettier extensions, point them at the project configuration, and enable format on save. Editor integration is a convenience, not a guarantee; CI still runs the same commands on a clean checkout.

A pre-commit hook should be fast: format and lint changed files, or run a quick check. Put the full test suite and build in CI. If a hook takes a minute, people will bypass it with --no-verify. A useful rule is that every check in the hook also exists in CI, so a local pass predicts a remote pass.

05Reproducible installs and semantic versioning

Commit package-lock.json. It records the exact dependency tree, including transitive packages, so npm ci can install the same versions on every machine. Use npm ci in CI and containers: it removes node_modules first and fails if the lockfile and package.json disagree. Use npm install when intentionally changing dependencies.

Semantic versioning communicates risk. Given 4.2.1, a patch release fixes bugs, a minor release adds compatible features, and a major release may break existing code. A caret range such as ^4.2.1 accepts minor and patch updates; a tilde accepts patch updates. Ranges are convenient, but lockfiles and tests are what make upgrades safe.

package.json (excerpt)JSON
{  "engines": { "node": ">=22 <25" },  "dependencies": { "some-library": "^4.2.1" },  "devDependencies": { "eslint": "^9.0.0" }}
LET IT CLICK

The ideas worth keeping.

  • Package scripts give people and CI one stable way to run the project.
  • ESLint protects correctness; Prettier removes formatting debates.
  • A committed lockfile plus deliberate semver ranges makes installs reproducible and upgrades reviewable.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Build a command interface

Add scripts for development with watch mode, production start, tests, linting, and one check command that runs lint before tests. Use Node's built-in tools where possible.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJSON
{  "type": "module",  "scripts": {    "dev": "node --watch src/server.js",    "start": "node src/server.js",    "test": "node --test",    "lint": "eslint .",    "check": "npm run lint && npm test"  }}

Each script names one job, and check composes existing scripts instead of duplicating commands. The && operator stops the chain when linting fails, so tests do not run against obviously broken code.

02

Exercise 2: Fail fast on bad configuration

Write a config module that requires API_URL and reads PORT with a default of 3000. Throw a descriptive error when API_URL is missing or PORT is not a positive integer.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const apiUrl = process.env.API_URL;if (!apiUrl) throw new Error('API_URL is required');const port = Number(process.env.PORT ?? 3000);if (!Number.isInteger(port) || port <= 0) {  throw new Error('PORT must be a positive integer');}export const config = Object.freeze({ apiUrl, port });

Validation happens once during startup, before the server accepts traffic. Object.freeze prevents later code from accidentally replacing a validated value.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01Which statement best describes the division of labour between ESLint and Prettier?
02Why should CI use npm ci instead of npm install?
03A dependency is declared as ^4.2.1. Which update is allowed?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Build a command interface

Add scripts for development with watch mode, production start, tests, linting, and one check command that runs lint before tests. Use Node's built-in tools where possible.

main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.