AI Hints
Home›CI/CD with GitHub Actions Tutorial›Fast Feedback and Node.js
CI/CD with GitHub Actions · Lesson 66 of 138

npm ci and Lockfiles

The same versions everywhere.

⏱ 2 min read▶ Video at 1:15:34🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Works on my machine often comes down to versions. Your laptop installed version two point one of a library, and CI installed two point two. A lock file records the exact versions, so everyone, including CI, installs the same thing.

npm ci and Lockfiles
From the video: npm ci and Lockfiles
  • Different versions, different results
  • A lock file records them
  • Same everywhere

The lock file

Package lock dot JSON is written by NPM install. Head shows the start of it. It records the exact version of everything installed. NPMCI installs exactly from the lock file, and refuses if it doesn't match package dot JSON, instead of quietly changing it.

Terminal
ls
cart.js  cart.test.js  package-lock.json  package.json
head -6 package-lock.json
{
  "name": "cart",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
npm ci && echo "installed from the lock file"
up to date in 79ms
installed from the lock file

install or ci?

Use NPM install on your machine when adding or updating packages. It may update the lock file. Use NPMCI in pipelines. It deletes node modules, installs exactly what's locked, never changes the lock file, and fails if anything is out of sync. Python has the same idea, with pinned requirements or lock files.

CommandUse it
npm installon your machine, to add or update
npm ciin CI, exact and clean
pip install -rwith pinned versions
Commit the lock filealways

Summary

Lock files pin exact versions, and NPMCI installs them exactly. Always commit the lock file. Next, scanning dependencies for security problems.

Key takeaways
  • package-lock.json
  • npm ci in CI
  • Commit the lock file
Next up: Dependency Scanning — known vulnerabilities