LoopBack 4项目部署至CentOS 8 VPS遇模块缺失问题求助
Hey there, let's work through this LoopBack 4 deployment issue together. That MODULE_NOT_FOUND error for @loopback/boot is happening because you only copied your local dist folder to the server—you missed installing the project dependencies on the VPS. Here's a complete, step-by-step deployment guide tailored to your CentOS 8 setup:
1. Confirm Preconfigured Environment
First, double-check the tools you mentioned are properly set up:
- Node.js: Ensure you're using an LTS version (16.x or 18.x recommended) that matches your local development environment. Run
node -vto verify. - MongoDB: Confirm the service is running with
systemctl status mongod, and set it to start on boot withsystemctl enable mongod. - PM2 & Nginx: These should already be installed and functional as you noted.
2. Pull Source Code from Bitbucket
Instead of transferring just the dist folder, pull the full project repository directly on the server to avoid environment mismatches:
- Install Git if it's not already present:
sudo dnf install git -y - Generate an SSH key for Bitbucket authentication (to avoid password prompts):
Copy the output public key and add it to your Bitbucket account's SSH keys settings.ssh-keygen -t ed25519 -C "your-email@example.com" cat ~/.ssh/id_ed25519.pub - Clone your project to a suitable directory (e.g.,
/var/www):cd /var/www git clone git@bitbucket.org:your-username/your-billing-project.git cd your-billing-project
3. Install Project Dependencies
This is the critical step you skipped earlier! LoopBack's core modules (like @loopback/boot) need to be installed on the server:
npm install
If you use Yarn locally, replace this with yarn install to keep consistency.
4. Compile the Project on the Server
Compile the TypeScript source to JavaScript directly on the VPS to avoid cross-environment issues:
npm run build
This will generate the dist folder with your compiled application code.
5. Configure Environment Variables
LoopBack relies on environment variables for database connections, port settings, and more. Create a .env file in your project root (copy your local .env and adjust for production):
NODE_ENV=production DATABASE_URL=mongodb://localhost:27017/billing-db PORT=3000
Ensure these values match your server's MongoDB setup and desired application port.
6. Start the App with PM2
Launch the compiled application using PM2, giving it a descriptive name for easy management:
pm2 start dist/index.js --name billing-service
- Check the app status:
pm2 status - View real-time logs:
pm2 logs billing-service(use this to troubleshoot any remaining issues)
7. Set Up PM2 Auto-Start on Boot
Make sure your app restarts automatically if the server reboots:
pm2 startup pm2 save
pm2 startup creates a system service, and pm2 save saves your current PM2 process list.
8. Configure Nginx Reverse Proxy
Route incoming HTTP traffic from your domain to the LoopBack app's port (3000 in this example):
- Create a new Nginx config file, e.g.,
/etc/nginx/conf.d/billing.conf:server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } - Test the Nginx configuration for errors:
nginx -t - Restart Nginx to apply changes:
systemctl restart nginx
9. Troubleshooting the Original Error
If you still encounter MODULE_NOT_FOUND:
- Verify you ran
npm installin the project root (check for anode_modulesfolder). - Ensure your local and server Node.js versions match (version mismatches can break dependency installs).
- Try a clean dependency install:
rm -rf node_modules package-lock.json npm install
内容的提问来源于stack exchange,提问作者Huan Ho

