Code Reference

Packaging

Node.js · Reference cheat sheet

Packaging

Node.js · Reference cheat sheet


📋 Overview

Node projects are defined by package.json: dependencies, scripts, entry points, and module format. Publish libraries to npm or run apps via scripts.

🔧 Core concepts

FieldPurpose
name / versionPackage identity (semver)
type"module" for ESM default
main / exportsEntry points
dependenciesRuntime deps
devDependenciesBuild/test-only deps
scriptsnpm run commands
enginesSupported Node versions
CommandEffect
npm installInstall from lockfile
npm ciClean CI install
npm outdatedCheck updates
npm packDry-run tarball

💡 Examples

Minimal library exports:

{
  "name": "@acme/utils",
  "version": "1.0.0",
  "type": "module",
  "exports": {
    ".": "./dist/index.js"
  },
  "files": ["dist"]
}

Useful scripts:

{
  "scripts": {
    "dev": "node --watch src/index.js",
    "start": "node src/index.js",
    "test": "node --test"
  }
}

Install exact tree in CI:

npm ci

⚠️ Pitfalls

  • Committing without a lockfile leads to non-reproducible installs.
  • Publishing secrets or .env via bad "files" / missing .npmignore is catastrophic.
  • Dual CJS/ESM packages need careful exports — test both consumers.

On this page