Skip to content

Commit ccdf03c

Browse files
committed
Fix PM2 'Script not found' error - always regenerate ecosystem config
- Fix ecosystem template: change script: 'start', interpreter: 'npm' to script: 'npm', args: 'start' - Add production defaults to generate_ecosystem_file(): autorestart, max_restarts, min_uptime - Remove stale config guards: always regenerate ecosystem.config.cjs on deploy - Ensures old broken configs (script: 'start') are fixed on next deployment
1 parent d9c42b3 commit ccdf03c

4 files changed

Lines changed: 53 additions & 63 deletions

File tree

lib/commands/deploy.sh

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,15 @@ ENDSSH
9292

9393
# Start or reload with PM2
9494
info "Starting application with PM2..."
95-
local PKG_START_CMD=$(get_pkg_start_cmd "$PKG_MANAGER" "$PM2_APP_NAME")
9695

97-
ssh -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" bash << ENDSSH
98-
set -e
99-
cd $REMOTE_PATH
100-
101-
# Delete and re-start to ensure PM2 picks up the correct cwd
102-
pm2 delete $PM2_APP_NAME 2>/dev/null || true
103-
104-
if [ -f ecosystem.config.js ]; then
105-
pm2 start ecosystem.config.js
106-
else
107-
$PKG_START_CMD
108-
fi
96+
# Always regenerate ecosystem file to ensure it's up to date
97+
info "Generating PM2 ecosystem config..."
98+
generate_ecosystem_file "$PKG_MANAGER" "$PM2_APP_NAME" "$REMOTE_PATH" \
99+
| ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "cat > $REMOTE_PATH/ecosystem.config.cjs"
109100

101+
ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" bash << ENDSSH
102+
set -e
103+
pm2 startOrReload $REMOTE_PATH/ecosystem.config.cjs --update-env
110104
pm2 save
111105
ENDSSH
112106

@@ -189,22 +183,14 @@ ENDSSH
189183
# Reload PM2
190184
info "Reloading application..."
191185

192-
# Generate PM2 start command based on package manager
193-
local PKG_START_CMD=$(get_pkg_start_cmd "$PKG_MANAGER" "$PM2_APP_NAME")
186+
# Always regenerate ecosystem file to ensure it's up to date
187+
info "Generating PM2 ecosystem config..."
188+
generate_ecosystem_file "$PKG_MANAGER" "$PM2_APP_NAME" "$REMOTE_PATH/current" \
189+
| ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "cat > $REMOTE_PATH/shared/ecosystem.config.cjs"
194190

195191
ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" bash << ENDSSH
196192
set -e
197-
cd $REMOTE_PATH/current
198-
199-
# Delete and re-start to ensure PM2 picks up the new release cwd
200-
pm2 delete $PM2_APP_NAME 2>/dev/null || true
201-
202-
if [ -f ecosystem.config.js ]; then
203-
pm2 start ecosystem.config.js
204-
else
205-
$PKG_START_CMD
206-
fi
207-
193+
pm2 startOrReload $REMOTE_PATH/shared/ecosystem.config.cjs --update-env
208194
pm2 save
209195
ENDSSH
210196

lib/commands/migrate.sh

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ cmd_migrate() {
2424

2525
local timestamp=$(generate_release_timestamp)
2626

27-
# Generate PM2 start command based on package manager
28-
local PKG_START_CMD=$(get_pkg_start_cmd "$PKG_MANAGER" "$PM2_APP_NAME")
27+
# Generate ecosystem file for backend apps (always regenerate to ensure it's up to date)
28+
if [ "$APP_TYPE" = "backend" ]; then
29+
info "Generating PM2 ecosystem config..."
30+
ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "mkdir -p $REMOTE_PATH/shared"
31+
generate_ecosystem_file "$PKG_MANAGER" "$PM2_APP_NAME" "$REMOTE_PATH/current" \
32+
| ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "cat > $REMOTE_PATH/shared/ecosystem.config.cjs"
33+
fi
2934

3035
ssh -T -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" bash << ENDSSH
3136
set -e
@@ -57,14 +62,8 @@ cmd_migrate() {
5762
5863
# Update PM2 to use current directory if backend
5964
if [ "$APP_TYPE" = "backend" ]; then
60-
cd current
6165
if pm2 describe $PM2_APP_NAME > /dev/null 2>&1; then
62-
pm2 delete $PM2_APP_NAME
63-
if [ -f ecosystem.config.js ]; then
64-
pm2 start ecosystem.config.js
65-
else
66-
$PKG_START_CMD
67-
fi
66+
pm2 startOrReload $REMOTE_PATH/shared/ecosystem.config.cjs --update-env
6867
pm2 save
6968
fi
7069
fi

lib/pkg-manager.sh

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ detect_pkg_manager() {
3232
}
3333

3434
# Get install command for package manager
35+
# Note: We install ALL dependencies (including dev) because many projects need
36+
# devDependencies for build tools (TypeScript, Prisma, etc.) during deployment.
37+
# Pruning devDependencies can happen after build/migration if needed.
3538
get_pkg_install_cmd() {
3639
local pkg_manager=$1
3740
case "$pkg_manager" in
3841
bun)
39-
echo "bun install --production"
42+
echo "bun install"
4043
;;
4144
pnpm)
42-
echo "pnpm install --prod"
45+
echo "pnpm install"
4346
;;
4447
yarn)
45-
echo "yarn install --production"
48+
echo "yarn install"
4649
;;
4750
*)
48-
echo "npm install --omit=dev"
51+
echo "npm install"
4952
;;
5053
esac
5154
}
@@ -70,24 +73,31 @@ get_pkg_run_cmd() {
7073
esac
7174
}
7275

73-
# Get PM2 start command for package manager
74-
get_pkg_start_cmd() {
76+
# Generate PM2 ecosystem file with symlink as cwd
77+
generate_ecosystem_file() {
7578
local pkg_manager=$1
7679
local app_name=$2
80+
local cwd=$3
81+
local interpreter
7782
case "$pkg_manager" in
78-
bun)
79-
echo "pm2 start bun --name \"$app_name\" -- start"
80-
;;
81-
pnpm)
82-
echo "pm2 start pnpm --name \"$app_name\" -- start"
83-
;;
84-
yarn)
85-
echo "pm2 start yarn --name \"$app_name\" -- start"
86-
;;
87-
*)
88-
echo "pm2 start npm --name \"$app_name\" -- start"
89-
;;
83+
bun) interpreter="bun" ;;
84+
pnpm) interpreter="pnpm" ;;
85+
yarn) interpreter="yarn" ;;
86+
*) interpreter="npm" ;;
9087
esac
88+
cat << EOF
89+
module.exports = {
90+
apps: [{
91+
name: "$app_name",
92+
script: "$interpreter",
93+
args: "start",
94+
cwd: "$cwd",
95+
autorestart: true,
96+
max_restarts: 10,
97+
min_uptime: '10s'
98+
}]
99+
};
100+
EOF
91101
}
92102

93103
# Install package manager on remote server
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
module.exports = {
22
apps: [{
33
name: 'APP_NAME',
4-
script: './index.js', // or your main entry file
5-
instances: 1,
6-
exec_mode: 'cluster',
4+
script: 'npm', // npm|yarn|pnpm|bun
5+
args: 'start',
6+
cwd: '/var/www/app/current',
77
env: {
88
NODE_ENV: 'production',
9-
PORT: APP_PORT
9+
PORT: 3000
1010
},
11-
error_file: './logs/error.log',
12-
out_file: './logs/output.log',
13-
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
14-
merge_logs: true,
1511
autorestart: true,
1612
max_restarts: 10,
17-
min_uptime: '10s',
18-
max_memory_restart: '500M'
13+
min_uptime: '10s'
1914
}]
2015
};

0 commit comments

Comments
 (0)