运行Vue项目npm run dev时出现未处理Promise拒绝错误求助
First, let’s unpack what exit code 3 typically means here: it’s a non-zero exit from a child process spawned by your Vue dev server, usually indicating a failure during initialization, dependency loading, or compilation. Since your project works locally but fails on EC2, let’s walk through the most likely fixes:
1. Ensure Dependencies Are Fully and Correctly Installed
Even if you’re using the same Node version, EC2’s environment might have missing or corrupted dependencies. Try these steps:
- Delete
node_modules,package-lock.json(oryarn.lock) from your EC2 instance:rm -rf node_modules package-lock.json - Reinstall dependencies with strict versioning and no optional packages (to avoid platform-specific mismatches):
npm install --no-optional --force - Verify that all dependencies listed in
package.jsonare compatible with your Node version on EC2. Some packages might have hidden version constraints that only surface on Linux.
2. Fix File/Directory Permissions
AWS EC2’s ubuntu user often lacks write access to certain directories, which can break the dev server’s ability to create temporary files or compile assets:
- Check the ownership of your project folder:
ls -la /home/ubuntu/loan-vue-frontend - If files are owned by
root, change ownership to yourubuntuuser:sudo chown -R ubuntu:ubuntu /home/ubuntu/loan-vue-frontend - Avoid running
npmcommands withsudounless absolutely necessary—this can create root-owned files that cause permission issues later.
3. Check for Missing Environment Variables
Your local setup might have a .env file with critical configuration (like API endpoints, keys) that wasn’t deployed to EC2:
- Create a
.envfile in your EC2 project directory matching your local one (ensure sensitive data is handled securely, e.g., using AWS Secrets Manager instead of hardcoding). - Verify that all variables referenced in your Vue project (like
process.env.VUE_APP_API_URL) are set correctly on EC2. You can print environment variables with:printenv | grep VUE_APP_
4. Match Node and npm Versions Exactly
Even minor differences in npm versions can cause dependency resolution issues. Use nvm (Node Version Manager) to lock in the exact Node and npm versions from your local setup:
- Install
nvmon EC2:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - Restart your terminal, then install your local Node version (6.11.3):
nvm install 6.11.3 nvm use 6.11.3 - Confirm the versions match your local machine:
node -v && npm -v
5. Get Full Error Logs
The truncated error message you shared doesn’t show the full stack trace. Run the dev command with verbose logging to get more details:
npm run dev --verbose
This will reveal exactly which child process is failing and why—whether it’s a Webpack compilation error, a missing module, or a configuration issue.
6. Check for Platform-Specific Code
Your project might have code that works on macOS/Windows but breaks on Linux (EC2):
- Look for hardcoded file paths using backslashes (
\) instead of forward slashes (/). - Check if any dependencies rely on OS-specific binaries (e.g.,
node-sasshas different builds for Linux vs macOS). Fornode-sass, you might need to rebuild it on EC2:npm rebuild node-sass
Start with these steps, and the full verbose logs should point you to the exact root cause.
内容的提问来源于stack exchange,提问作者murvinlai

