Lockfiles & installs

Lockfile & Installs

In a Node.js project, the process of installing packages and the role of the lock file are critical for ensuring that an application runs consistently across different environments (like your local machine and an AWS server).

1. The npm install Process


When you run npm install (or npm i), several things happen:

  1. Dependency Resolution : Node looks at your package.json.

  2. Downloading                       : It fetches the modules from the npm registry.

  3. Storage                                  : It places the code into the node_modules folder.

  4. Creation/Update                : It creates or updates the package-lock.json file.

Install Types

  • Local Install                         : npm install <package> — Adds the package to the current project's node_modules.

  • Global Install                       : npm install -g <package> — Installs the package system-wide (rarely used now, as npx is preferred).

  • Production Install              : npm install --production — Only installs dependencies, skipping devDependencies. This is used when deploying to servers to keep the application size small.


2. The Role of package-lock.json


While package.json uses ranges for versions (e.g., "^1.2.0" means version 1.2.0 or any higher minor/patch version), the lock file is a snapshot of the exact versions currently installed.


Why the Lock File is Vital


  • Deterministic Installs: It guarantees that if two people run npm install at different times, they get the exact same code. Without it, one person might get version 1.2.0 and another might get 1.2.5, which could introduce bugs.


  • Dependency Tree Security: It tracks the versions of "sub-dependencies" (the packages your packages rely on).

  • Faster Installs: Since the structure of the node_modules is already calculated in the lock file, npm can skip the "resolution" step and go straight to downloading.


3. npm install vs. npm ci


In professional workflows, especially in CI/CD (Continuous Integration/Continuous Deployment), these two commands serve different purposes:


  • npm install:

    • Can update the package-lock.json.

    • Good for development when you are adding or updating packages.


  • npm ci (Clean Install):

    • Never updates the lock file. If the lock file and package.json don't match, it throws an error.

    • Deletes the existing node_modules and does a fresh, exact install.

    • Use this on your server or deployment pipelines to ensure 100% consistency.


4. Semantic Versioning (SemVer) Symbols


In your package.json, you will see symbols next to version numbers that dictate how the lock file handles updates during an install:

  • ^ (Caret): "^1.2.3" — Allows updates to Minor and Patch versions (e.g., 1.3.0). Most common.

  • ~ (Tilde): "~1.2.3" — Allows updates only to Patch versions (e.g., 1.2.4).

  • No Symbol: "1.2.3" — Locks the version strictly. Even npm install won't update it.


5. Best Practices


  • Always Commit the Lock File: package-lock.json should always be pushed to GitHub/GitLab.

  • Ignore node_modules: Never commit the actual node_modules folder. Use a .gitignore file to skip it.

  • Audit Regularly: Use npm audit to check if your installed packages have known security vulnerabilities.