How to Stop “Cannot Find Module” Errors When NestJS Runs on a Shared Hosting VPS – The Frustrating, Overlooked Fix That Saves 3 Hours Every Deployment Cycle
You've just pushed your latest NestJS micro‑service to a cheap VPS, hit node main.js, and the console erupts with “Cannot find module ‘@nestjs/core’”. Your heart drops, the deadline looms, and you start Googling “NestJS shared hosting error”. Spoiler: the fix is a single line you probably missed during the last deployment.
Why This Matters
Every extra minute you spend hunting down missing dependencies is a minute you’re not billing a client, not optimizing a CI pipeline, and not scaling your side‑hustle. On shared hosting or low‑cost VPS plans, the environment is stripped down to the basics, which means npm install behaves differently than on your local macOS/Windows dev box. The result? A silent “module not found” that can waste up to three hours per release.
Step‑by‑Step Tutorial: The Overlooked Fix
-
Confirm Your Node Version
Shared hosts often run an older Node LTS. Run
node -von the VPS. If it’sv12or lower, you need at leastv14for NestJS 9+. Upgrade withnvmor ask your provider. -
Create a Production‑Ready
package.jsonMake sure all NestJS packages are listed under
dependencies, notdevDependencies. The runtime environment never installs dev dependencies.// package.json excerpt { "dependencies": { "@nestjs/common": "^9.0.0", "@nestjs/core": "^9.0.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.5.0" }, "devDependencies": { "@nestjs/cli": "^9.0.0", "typescript": "^4.8.0" } } -
Add a Post‑Install Script to Rebuild Node Modules
Shared hosts sometimes clear caches between deployments. Adding a script guarantees a clean
node_modulestree every time.// package.json addition "scripts": { "postinstall": "npm prune && npm rebuild" } -
Use
npm ciInstead ofnpm installnpm cireads the exact versions frompackage-lock.jsonand skips optional peer‑dependency warnings that can break the install on stripped‑down systems.Tip: Add--productionflag if you’re certain you don’t need any dev deps at runtime. -
Set the Correct
NODE_PATHThe hidden culprit is often the
NODE_PATHenvironment variable. On a minimal VPS it defaults to/usr/lib/node_modules, which doesn’t include your project’snode_modules. Export the path in your.bashrcor inside the start script:# Inside start.sh export NODE_PATH=$PWD/node_modules node dist/main.jsWarning: ForgettingNODE_PATHis the #1 reason “Cannot find module” appears only on production. -
Compile TypeScript on the Server (Optional)
If you prefer to ship raw
.tsfiles, add a build step that runsnpm run buildafternpm ci. The compiled JavaScript lands indist/, and the start command becomesnode dist/main.js. -
Verify Permissions
Shared hosts sometimes assign
www-dataas the running user. Ensurenode_modulesis world‑readable:chmod -R 755 node_modules
Real‑World Use Case: Deploying a SaaS API on a $5/month VPS
Jane runs a subscription‑based analytics API built with NestJS. She uses a $5 DigitalOcean droplet with Ubuntu 22.04. Before the fix, each release threw “Cannot find module ‘@nestjs/microservices’”. After adding the NODE_PATH export and switching to npm ci --production, deployments went from 3 hours to under 10 minutes. Revenue‑critical endpoints stayed live, and her churn rate dropped by 4%.
Results / Outcome
- Eliminated “Cannot find module” errors on every deployment.
- Reduced deployment time from ~180 minutes to under 10 minutes.
- Improved uptime on shared hosting – no more 30‑minute blank‑out windows.
- Saved enough developer time to add a new feature that generated an extra $2,300/mo.
Bonus Tips (Grab the Edge)
- Use
pm2with--env productionto auto‑restart after crashes. - Pin your Node version in
.nvmrcand addnvm installto the deployment script. - Store the compiled
dist/folder in a separate Git branch for quick rollback. - Enable
skip-filesin.npmrcto avoid copying large dev assets to the server.
Monetization Hint: Turn This Fix into a Mini‑Service
If you’re a freelancer, package the “VPS‑Ready NestJS Deploy Script” as a paid add‑on on Fiverr or Upwork. Clients love a one‑click solution that guarantees zero module errors. At $49 per deployment, you can easily recoup the time you saved.
Ready to stop the “Cannot find module” nightmare? Apply the steps above on your next push and watch the deployment clock tumble. Happy coding!
No comments:
Post a Comment