Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: Exercises and Cheat Sheet

Practice publishing, lockfiles, security and monorepos with runnable exercises, then keep a cheat sheet of commands and a table of common install errors and their fixes.

You have covered publishing, lockfiles, security, monorepos and performance. This final lesson makes it stick: four exercises you can run and change, a cheat sheet to keep open while you work, and a troubleshooting table for the errors you will certainly meet.

Each exercise is a runnable Node.js program with a few checks at the end. Each block already holds a working solution: to practise, delete the function body, rewrite it yourself and press run until every check prints ok. The text below each exercise explains the reasoning.

Exercise 1: which package manager does this project use?

Teams often inherit a repository and have to guess its package manager. Write detect(files, manifest) that returns the manager name using this priority: the packageManager field wins; otherwise look at lockfiles (pnpm-lock.yaml, yarn.lock, package-lock.json, bun.lock); if there are several lockfiles, report a conflict; if none, return npm as the default.

Node.js Exercise 1: detect the package manager

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Why the packageManager field wins: it is an explicit declaration, while a lockfile can be left behind by accident (see Corepack and alternatives). Extension: have it return the right CI install command as well.

Exercise 2: find missing and unused dependencies

A classic hygiene task: find packages that code imports but package.json does not declare (they only work through hoisting, see node_modules layouts), and packages declared but never imported. Write analyze(sources, pkg). Remember that built-in modules (node:fs, path) and relative imports do not count, and that scoped packages keep their scope (@acme/ui/button belongs to @acme/ui).

Node.js Exercise 2: missing and unused dependencies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The real tools for this are depcheck, knip and ESLint's import/no-extraneous-dependencies. Beware of false alarms: packages used only in config files, CLIs called from scripts and type packages (@types/*) are "unused" by import scanning but needed.

Exercise 3: report duplicated packages in a lockfile

Duplicate versions inflate installs and bundles. Given the packages of a lockfile (install path to version), list every package name with more than one version and the number of copies, sorted by copies, largest first.

Node.js Exercise 3: find duplicates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice [email protected] appears twice with the same version: the copies are identical but not shared, because nesting forced a physical duplicate. Commands that deal with this in practice: npm dedupe, yarn dedupe, pnpm dedupe. Different versions can only be merged when the ranges allow it; otherwise an override (overrides and resolutions) might help.

Exercise 4: a pre-publish gate

Combine what you learned into a gate that decides whether a release may proceed. It receives the manifest, the list of files in the tarball, and the audit counts, and returns a list of blocking problems. Rules: private must not be true; version must not already exist on the registry; the tarball must contain LICENSE and no .env; and no high or critical vulnerabilities are allowed in production dependencies.

Node.js Exercise 4: the release gate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Challenge: make the gate read the real package.json and npm pack --json output, and run it as the prepublishOnly script of the CLI from the small CLI project.

Cheat sheet

Daily commands

Task npm Yarn (Berry) pnpm
Strict install in CI npm ci yarn install --immutable pnpm install --frozen-lockfile
Why is this installed? npm explain pkg yarn why pkg pnpm why pkg
Outdated packages npm outdated yarn upgrade-interactive pnpm outdated
Dedupe the tree npm dedupe yarn dedupe pnpm dedupe
Audit npm audit yarn npm audit pnpm audit
Pack a tarball npm pack yarn pack pnpm pack
Publish npm publish yarn npm publish pnpm publish
Run in one workspace npm run x -w pkg yarn workspace pkg run x pnpm --filter pkg run x
Run in all workspaces npm run x -ws yarn workspaces foreach -A run x pnpm -r run x

Release and security checklist

  1. npm ci, tests, build.
  2. npm pack --dry-run: read the file list.
  3. npm audit --omit=dev --audit-level=high.
  4. Bump the version (npm version, or change files in a monorepo).
  5. Publish with two-factor authentication or trusted publishing; add --provenance in supported CI.
  6. Verify in an empty folder: npm install name@latest.
  7. Never reuse versions; deprecate bad ones.

Troubleshooting common errors

Error Usual cause Fix
ERESOLVE unable to resolve dependency tree Conflicting peer dependency ranges Read the message; upgrade or downgrade the named package so the peers agree; --legacy-peer-deps or overrides only as a conscious last resort
EINTEGRITY / checksum failed Corrupted cache or a tarball changed under a pinned hash npm cache verify, retry; if it persists, investigate the registry/proxy (do not simply delete the hash)
EACCES permission denied on global install Global prefix owned by root Use a Node version manager or set a user-owned prefix; do not use sudo npm
ENOENT on package.json Running in the wrong folder cd to the project, or use --prefix / -w
npm ci can only install with an existing lockfile / out of sync Lockfile and manifest disagree Run npm install locally, commit the lockfile
E403 / E404 when publishing a scoped package Not logged in, no rights, or first scoped publish without --access public npm whoami, check scope ownership, add --access public
E402 payment required Private scoped package on a free plan Publish public, or upgrade the plan
Cannot find module 'x' under pnpm Undeclared (phantom) dependency pnpm add x in the package that imports it
Invalid hook call after linking a library Two copies of React Make both sides resolve one React (alias or link it)
EBADENGINE warning Node version outside engines Switch Node version (.nvmrc) or relax engines honestly

When something is wrong and you do not know why, three commands answer most questions: npm ls name (what resolved and where), npm explain name (why it is there), and npm config list (which registry and settings are active).

Where to go from here

  • Read your own lockfile diff on the next pull request.
  • Publish a package to a private or local registry (Verdaccio) as a safe rehearsal.
  • Convert a small project between managers and compare layout and install time.
  • Put the exercises' logic into a real script in your CI.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A repository contains both package-lock.json and yarn.lock and no packageManager field. What is the best first step?

    Two lockfiles mean two possible trees. Pick one manager, remove the other lockfile and pin it so the conflict does not return.
  2. 2. An import scanner reports @types/node as unused. What is the right conclusion?

    Type declaration packages are consumed by TypeScript without an import statement, so import scanning cannot see them.
  3. 3. ERESOLVE appears during install. Which response is best?

    ERESOLVE reports a real conflict. Fixing the versions is safer than forcing an inconsistent tree.
  4. 4. Which check belongs in a pre-publish gate?

    A gate should block avoidable, costly mistakes: leaked secrets, reused versions, missing licenses and known high-severity issues.

Summary

  • Detecting the manager, scanning imports, finding duplicates and gating releases are small programs built from ideas in this course.
  • Keep the cheat sheet of equivalent commands for npm, Yarn and pnpm at hand.
  • Most install errors have a short list of causes: conflicting peers, out-of-sync lockfiles, permissions, undeclared dependencies and mismatched Node versions.
  • Prefer fixing the cause over forcing the install, and verify releases from an empty folder.

Next: Back to the course overview