@@ -393,21 +393,84 @@ else
393393 [ ! -z " $https_proxy " ] && npm set https-proxy=" $https_proxy "
394394 [ ! -z " $no_proxy " ] && npm set noproxy=" $no_proxy "
395395 echo " Installing npm version ${NPM_VERSION} ..."
396+
397+ CURRENT_NPM_VERSION=$( npm --version 2> /dev/null || echo ' unknown' )
398+ echo " Current npm version: $CURRENT_NPM_VERSION "
396399
397- # Clear npm cache to avoid conflicts
400+ # Clear npm cache and extract version numbers
398401 npm cache clean --force 2> /dev/null || true
402+ CURRENT_MAJOR=$( echo " $CURRENT_NPM_VERSION " | cut -d. -f1 || echo " 0" )
403+ NODE_MAJOR=$( node --version 2> /dev/null | cut -d. -f1 | tr -d ' v' || echo " 0" )
404+
405+ # Dynamically check npm's Node.js requirements and auto-fallback if incompatible
406+ ORIGINAL_NPM_VERSION=" $NPM_VERSION "
407+ if [ " $NPM_VERSION " != " none" ]; then
408+ echo " Checking npm compatibility requirements..."
409+ NPM_NODE_REQUIREMENT=$( npm view npm@${NPM_VERSION} engines.node 2> /dev/null || echo " " )
410+
411+ if [ -n " $NPM_NODE_REQUIREMENT " ]; then
412+ echo " npm $NPM_VERSION requires Node.js: $NPM_NODE_REQUIREMENT "
413+
414+ # Extract minimum required Node version from requirement string
415+ MIN_NODE=$( echo " $NPM_NODE_REQUIREMENT " | grep -oE ' [0-9]+' | head -1 || echo " 0" )
416+
417+ if [ " $MIN_NODE " -gt " 0" ] && [ " $NODE_MAJOR " -lt " $MIN_NODE " ]; then
418+ echo " ⚠️ WARNING: npm $NPM_VERSION requires Node.js $MIN_NODE +, you have $NODE_MAJOR .x"
419+
420+ # Find compatible npm version dynamically using same logic
421+ echo " 🔍 Finding compatible npm version for Node.js $NODE_MAJOR .x..."
422+
423+ # Try npm major versions in descending order to find highest compatible version
424+ for npm_major in 10 9 8 7 6; do
425+ echo " Checking npm $npm_major compatibility..."
426+ FALLBACK_NODE_REQUIREMENT=$( npm view " npm@${npm_major} " engines.node 2> /dev/null || echo " " )
427+
428+ if [ -n " $FALLBACK_NODE_REQUIREMENT " ]; then
429+ MIN_NODE=$( echo " $FALLBACK_NODE_REQUIREMENT " | grep -oE ' [0-9]+' | head -1 || echo " 0" )
430+
431+ if [ " $MIN_NODE " -le " $NODE_MAJOR " ]; then
432+ # Get latest patch version for this compatible major version
433+ NPM_VERSION=$( npm view " npm@${npm_major} " version 2> /dev/null || echo " " )
434+ if [ -n " $NPM_VERSION " ]; then
435+ echo " ✓ Found compatible npm $NPM_VERSION (requires Node.js $MIN_NODE +)"
436+ echo " 🔄 Auto-fallback: Installing compatible npm $NPM_VERSION instead"
437+ break
438+ fi
439+ fi
440+ fi
441+ done
442+
443+ # If no compatible version found, skip npm installation
444+ if [ " $NPM_VERSION " = " $ORIGINAL_NPM_VERSION " ]; then
445+ echo " ❌ Could not find compatible npm version, keeping current npm"
446+ NPM_VERSION=" none"
447+ fi
448+ elif [ " $MIN_NODE " -gt " 0" ]; then
449+ echo " ✓ Node.js $NODE_MAJOR .x meets npm $NPM_VERSION requirement"
450+ fi
451+ else
452+ echo " Could not determine Node.js requirements for npm $NPM_VERSION , proceeding anyway..."
453+ fi
454+ fi
455+
456+ # Use special upgrade method for npm 10.x to latest (only if not falling back)
457+ if [ " $ORIGINAL_NPM_VERSION " = " latest" ] && [ " $NPM_VERSION " = " latest" ] && [ " $CURRENT_MAJOR " = " 10" ]; then
458+ echo " Using npmjs.org install script for npm upgrade"
459+ curl -fsSL https://www.npmjs.com/install.sh | sh 2> /dev/null || true
460+ fi
399461
400462 # Try npm installation with retries
401463 for i in {1..3}; do
402- if npm install -g npm@$NPM_VERSION --force; then
403- echo " Successfully installed npm@${NPM_VERSION} "
464+ echo " Attempt $i : Running npm install -g npm@$NPM_VERSION "
465+ if npm install -g npm@$NPM_VERSION --force --no-audit --no-fund 2>&1 ; then
466+ NEW_VERSION=$( npm --version 2> /dev/null || echo ' unknown' )
467+ echo " Successfully installed npm@${NPM_VERSION} , new version: $NEW_VERSION "
404468 break
405469 else
406470 echo " Attempt $i failed, retrying..."
407471 sleep 2
408472 if [ $i -eq 3 ]; then
409- echo " Failed to install npm@${NPM_VERSION} after 3 attempts. Trying latest npm as fallback..."
410- npm install -g npm@latest --force || echo " Fallback to latest npm also failed. Keeping current npm version $( npm --version 2> /dev/null || echo ' unknown' ) ."
473+ echo " Failed to install npm@${NPM_VERSION} after 3 attempts. Keeping current npm version $( npm --version 2> /dev/null || echo ' unknown' ) ."
411474 fi
412475 fi
413476 done
0 commit comments