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.
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.
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)
-
Connect to Your VPS
Open your terminal and SSH into the VPS:
ssh user@your-vps-ip -
Update the Package Index
Make sure you have the latest package lists:
sudo apt-get update -y -
Install Build Essentials
The
bcryptpackage compiles native C++ code. You needgcc,g++, andmake:sudo apt-get install -y build-essential -
Install OpenSSL Development Headers
bcryptlinks against OpenSSL. Install the dev package:sudo apt-get install -y libssl-dev -
Navigate to Your Project Folder
Usually something like
/var/www/your-app:cd /var/www/your-app -
Clean Node Modules & Reinstall
Delete the old
node_modulesfolder and reinstall withnpm ci(ornpm installif you don’t have a lock file):rm -rf node_modules npm ci -
Verify the Build
Run a quick node script to ensure
bcryptloads:node -e "require('bcrypt'); console.log('bcrypt works');" -
Start Your NestJS App
Now fire up the production build:
npm run start:prodIf everything is in place, you’ll see the usual “Nest application listening on …” message instead of the dreaded error.
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.
No comments:
Post a Comment