The AUDIT_EXIT_CODE variable is set after the command completion, but $? may not reflect the correct exit code due to the assignment operation. Store the exit code immediately: npm audit --json 2>&1; AUDIT_EXIT_CODE=$?; AUDIT_OUTPUT=$(npm audit --json 2>&1) or use a different approach to capture both output and exit code reliably.
# Run npm audit and capture output and exit code reliably
npm audit --json 2>&1 | tee audit_output.json > /dev/null
AUDIT_EXIT_CODE=$?
AUDIT_OUTPUT=$(cat audit_output.json)
rm -f audit_output.json
Originally posted by @Copilot in #4025 (comment)