Monday, May 4, 2026

Debugging “ModuleNotFoundError: Cannot find module 'bcrypt'” in NestJS on a Shared VPS: Why Your Production Build Crashes and How to Fix It in 10 Minutes or Less

Debugging “ModuleNotFoundError: Cannot find module 'bcrypt'” in NestJS on a Shared VPS: Why Your Production Build Crashes and How to Fix It in 10 Minutes or Less

You've just pushed a brand‑new NestJS microservice to your shared VPS, hit npm start, and—boom—your app crashes with ModuleNotFoundError: Cannot find module 'bcrypt'. Panic sets in, the deadline looms, and you wonder if you just wasted hours of coding for nothing. Trust me, you’re not alone. This error is a classic when you move from a local dev box (macOS, Windows) to a Linux‑based VPS that doesn’t have the same native build tools.

Quick TL;DR: The VPS is missing gcc/make or the proper OpenSSL headers. Install them, rebuild bcrypt, and your NestJS app will start without screaming.

Why This Matters

In production, every second of downtime costs money—and credibility. A missing native module like bcrypt doesn’t just stop authentication, it stops the entire HTTP server from booting. That means zero traffic, zero revenue, and a frantic support ticket that could have been avoided with a few simple server‑side checks.

Pro tip: Always run npm ci on a clean server copy. It forces a fresh install of every dependency, exposing native‑module issues before they hit your users.

Step‑by‑Step Tutorial (Fix in <10 minutes)

  1. Connect to Your VPS

    Open your terminal and SSH into the VPS:

    ssh user@your-vps-ip
  2. Update the Package Index

    Make sure you have the latest package lists:

    sudo apt-get update -y
  3. Install Build Essentials

    The bcrypt package compiles native C++ code. You need gcc, g++, and make:

    sudo apt-get install -y build-essential
  4. Install OpenSSL Development Headers

    bcrypt links against OpenSSL. Install the dev package:

    sudo apt-get install -y libssl-dev
  5. Navigate to Your Project Folder

    Usually something like /var/www/your-app:

    cd /var/www/your-app
  6. Clean Node Modules & Reinstall

    Delete the old node_modules folder and reinstall with npm ci (or npm install if you don’t have a lock file):

    rm -rf node_modules
    npm ci
  7. Verify the Build

    Run a quick node script to ensure bcrypt loads:

    node -e "require('bcrypt'); console.log('bcrypt works');"
  8. Start Your NestJS App

    Now fire up the production build:

    npm run start:prod

    If everything is in place, you’ll see the usual “Nest application listening on …” message instead of the dreaded error.

Warning: Skipping step 4 (OpenSSL headers) will still let npm ci finish, but bcrypt will throw a runtime warning and behave unreliably.

Code Example: Using Bcrypt in a NestJS Auth Guard

import { Injectable } from '@nestjs/common';
import * as bcrypt from 'bcrypt';

@Injectable()
export class AuthService {
  async hashPassword(plain: string): Promise<string> {
    const salt = await bcrypt.genSalt(12);
    return bcrypt.hash(plain, salt);
  }

  async comparePassword(
    plain: string,
    hash: string,
  ): Promise<boolean> {
    return bcrypt.compare(plain, hash);
  }
}

Real‑World Use Case: SaaS Login on a Shared VPS

A small SaaS startup runs a NestJS API on a $5/month shared VPS. Their customers sign up, passwords are hashed with bcrypt, and the API serves 200 requests per minute. After a weekend deployment, the login endpoint started returning 500 errors. The logs pointed straight to the missing bcrypt module. By applying the steps above, they got the service back online in under 7 minutes—saving an estimated $150 in lost subscriptions.

Results / Outcome

  • Zero downtime: The app boots cleanly after the fix.
  • Consistent hashing: Passwords are securely salted with 12 rounds.
  • Scalable: The same steps work on any Linux‑based VPS, from cheap shared hosts to high‑end droplets.
  • Cost savings: Avoid costly support tickets and subscription churn.

Bonus Tips

Tip 1 – Use the Pure‑JS Alternative in Production

If you cannot install native build tools on a constrained host, swap bcrypt for bcryptjs. It’s slower but requires no compilation:

npm uninstall bcrypt
npm install bcryptjs

Tip 2 – Pre‑build Docker Image

Build a Docker image locally (or in CI) that already contains the compiled bcrypt binary. Deploy the same image to the VPS to eliminate native‑module surprises.

Tip 3 – Automate the Check

Add a startup script that verifies critical native modules:

#!/bin/bash
node -e "try{require('bcrypt');}catch(e){echo 'bcrypt missing!'; exit 1;}"
if [ $? -ne 0 ]; then
  echo "🔧 Installing build tools..."
  sudo apt-get install -y build-essential libssl-dev
  npm ci
fi
npm run start:prod

Monetization Add‑On (Optional)

If you run a DevOps consultancy or sell NestJS starter kits, bundle this “Production‑Ready Native Module Checklist” as a downloadable PDF. Charge $9.99 per copy or include it as a premium upgrade for your SaaS customers. The perceived value is high because it prevents costly downtime.

Bottom line: Missing native dependencies are a silent killer on shared VPS environments. A few apt‑get commands, a clean reinstall, and you’ll have a rock‑solid NestJS app that hashes passwords securely—no more “module not found” panic attacks.

No comments:

Post a Comment