Saturday, May 2, 2026

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

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

  1. Confirm Your Node Version

    Shared hosts often run an older Node LTS. Run node -v on the VPS. If it’s v12 or lower, you need at least v14 for NestJS 9+. Upgrade with nvm or ask your provider.

  2. Create a Production‑Ready package.json

    Make sure all NestJS packages are listed under dependencies, not devDependencies. 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"
      }
    }
    
  3. Add a Post‑Install Script to Rebuild Node Modules

    Shared hosts sometimes clear caches between deployments. Adding a script guarantees a clean node_modules tree every time.

    // package.json addition
    "scripts": {
      "postinstall": "npm prune && npm rebuild"
    }
    
  4. Use npm ci Instead of npm install

    npm ci reads the exact versions from package-lock.json and skips optional peer‑dependency warnings that can break the install on stripped‑down systems.

    Tip: Add --production flag if you’re certain you don’t need any dev deps at runtime.
  5. Set the Correct NODE_PATH

    The hidden culprit is often the NODE_PATH environment variable. On a minimal VPS it defaults to /usr/lib/node_modules, which doesn’t include your project’s node_modules. Export the path in your .bashrc or inside the start script:

    # Inside start.sh
    export NODE_PATH=$PWD/node_modules
    node dist/main.js
    
    Warning: Forgetting NODE_PATH is the #1 reason “Cannot find module” appears only on production.
  6. Compile TypeScript on the Server (Optional)

    If you prefer to ship raw .ts files, add a build step that runs npm run build after npm ci. The compiled JavaScript lands in dist/, and the start command becomes node dist/main.js.

  7. Verify Permissions

    Shared hosts sometimes assign www-data as the running user. Ensure node_modules is 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 pm2 with --env production to auto‑restart after crashes.
  • Pin your Node version in .nvmrc and add nvm install to the deployment script.
  • Store the compiled dist/ folder in a separate Git branch for quick rollback.
  • Enable skip-files in .npmrc to 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