使用pm2启动NodeJS项目后网站无法正常运行求助
Let’s walk through the most common reasons your app works with yarn start but not PM2, and how to fix them:
1. You’re Pointing PM2 to the Wrong File
First off, double-check the path you’re passing to PM2. When you run yarn start, it’s executing dist/app.js, but if you run pm2 start app.js from your project root, PM2 is looking for an app.js file in the root directory—not inside the dist folder. That’s a super easy mistake to make!
Try running this instead:
pm2 start dist/app.js
2. PM2’s Working Directory Doesn’t Match Your Project Root
Even if you point to dist/app.js, PM2 might be using a different working directory than when you run yarn start. This breaks things like relative paths for config files, assets, or environment variables that rely on your project’s root folder.
Fix this by explicitly setting the working directory when starting PM2:
pm2 start dist/app.js --cwd /path/to/your/project/root
Or create a permanent PM2 config file (ecosystem.config.js) to avoid repeating this:
module.exports = { apps: [{ name: "your-app-name", script: "./dist/app.js", cwd: "/path/to/your/project/root", // Add other configs like env vars here }] };
Then start your app with:
pm2 start ecosystem.config.js
3. Environment Variables Aren’t Loading
yarn start might be pulling in env vars from a .env file or your shell’s profile, but PM2 doesn’t automatically inherit all shell variables or load .env files by default.
- If you use a
.envfile, make suredotenvis installed in your project, and add this to the top ofdist/app.js:require('dotenv').config(); - Alternatively, pass env vars directly in the PM2 command or config:
Or add to your ecosystem config:pm2 start dist/app.js --env productionenv: { NODE_ENV: "development", API_KEY: "your-secret-key" }, env_production: { NODE_ENV: "production" }
4. Missing Dependencies or Stale Build Files
Sometimes yarn start includes a build step (like running yarn build to generate the dist folder) that you’re skipping when using PM2.
- Run
yarn install --productionto ensure all necessary production dependencies are installed (no unused dev dependencies cluttering things up). - Re-run
yarn buildto make sure yourdistfolder has fresh, complete build artifacts—silent build failures are more common than you think!
5. Check PM2 Logs for Exact Errors
If none of the above works, PM2’s logs will tell you exactly what’s broken. It captures stdout and stderr from your app, so you’ll see errors like missing modules, invalid configs, or port conflicts.
View the logs with:
pm2 logs your-app-name
Or see the last 50 lines quickly:
pm2 logs --lines 50
This is almost always the fastest way to find the root cause.
6. Node.js Version Mismatch
PM2 might be using a different Node.js version than your shell (especially if you use nvm or fnm). Check which version PM2 is using:
pm2 show your-app-name | grep node_version
Compare it to the version you use with yarn start:
node -v
If they differ, set the correct Node path in your ecosystem config:
exec_interpreter: "/path/to/your/node/bin/node"
内容的提问来源于stack exchange,提问作者Sid

