Skip to content

Commit b680114

Browse files
committed
fix: improve npm version check with proper environment handling
- Set NODE_PATH when running npm from nvm installation - Add fallback to check npm version via node if direct execution fails - Better error messages showing exit code and stderr - Handle npm execution failures more gracefully Fixes issue where npm is found but version check fails due to missing environment variables in Jupyter kernel.
1 parent fb650ca commit b680114

1 file changed

Lines changed: 64 additions & 2 deletions

File tree

notebooks/setup/complete_setup_guide.ipynb

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,79 @@
117117
" return False, None, f\"❌ {command} is not installed or not in PATH\"\n",
118118
" \n",
119119
" try:\n",
120+
" # For npm, ensure we have proper environment (especially for nvm)\n",
121+
" env = os.environ.copy()\n",
122+
" # If npm is in nvm path, ensure NODE_PATH is set\n",
123+
" if 'nvm' in command_path and command == 'npm':\n",
124+
" nvm_dir = os.path.dirname(os.path.dirname(command_path))\n",
125+
" if 'NODE_PATH' not in env:\n",
126+
" env['NODE_PATH'] = nvm_dir\n",
127+
" \n",
120128
" result = subprocess.run(\n",
121129
" [command_path, version_flag],\n",
122130
" capture_output=True,\n",
123131
" text=True,\n",
124-
" timeout=5\n",
132+
" timeout=5,\n",
133+
" env=env\n",
125134
" )\n",
126135
" if result.returncode != 0:\n",
127-
" return False, None, f\"❌ {command} found but version check failed\"\n",
136+
" # For npm, try alternative: use node to check npm version\n",
137+
" if command == 'npm':\n",
138+
" try:\n",
139+
" # Try: node -e \"console.log(require('npm/package.json').version)\"\n",
140+
" node_dir = os.path.dirname(command_path)\n",
141+
" node_path = os.path.join(node_dir, 'node')\n",
142+
" if os.path.exists(node_path):\n",
143+
" alt_result = subprocess.run(\n",
144+
" [node_path, '-e', \"console.log(require('npm/package.json').version)\"],\n",
145+
" capture_output=True,\n",
146+
" text=True,\n",
147+
" timeout=5,\n",
148+
" env=env\n",
149+
" )\n",
150+
" if alt_result.returncode == 0:\n",
151+
" version = alt_result.stdout.strip()\n",
152+
" return True, version, f\"✅ {command} found: {version}\"\n",
153+
" except:\n",
154+
" pass\n",
155+
" return False, None, f\"❌ {command} found but version check failed (exit code: {result.returncode}, stderr: {result.stderr[:100]})\"\n",
128156
" version = result.stdout.strip() or result.stderr.strip()\n",
129157
" return True, version, f\"✅ {command} found: {version}\"\n",
130158
" except Exception as e:\n",
159+
" # For npm, if direct execution fails, try finding it via node\n",
160+
" if command == 'npm':\n",
161+
" try:\n",
162+
" node_ok, node_version, node_msg = check_command('node')\n",
163+
" if node_ok:\n",
164+
" # Find node's directory and check for npm there\n",
165+
" node_path_result = subprocess.run(\n",
166+
" ['which', 'node'],\n",
167+
" capture_output=True,\n",
168+
" text=True,\n",
169+
" timeout=5\n",
170+
" )\n",
171+
" if node_path_result.returncode == 0:\n",
172+
" node_path = node_path_result.stdout.strip()\n",
173+
" node_dir = os.path.dirname(node_path)\n",
174+
" npm_candidate = os.path.join(node_dir, 'npm')\n",
175+
" if os.path.exists(npm_candidate):\n",
176+
" # Try running npm with proper environment\n",
177+
" env = os.environ.copy()\n",
178+
" nvm_dir = os.path.dirname(os.path.dirname(node_path))\n",
179+
" if 'NODE_PATH' not in env:\n",
180+
" env['NODE_PATH'] = nvm_dir\n",
181+
" npm_result = subprocess.run(\n",
182+
" [npm_candidate, '--version'],\n",
183+
" capture_output=True,\n",
184+
" text=True,\n",
185+
" timeout=5,\n",
186+
" env=env\n",
187+
" )\n",
188+
" if npm_result.returncode == 0:\n",
189+
" version = npm_result.stdout.strip()\n",
190+
" return True, version, f\"✅ {command} found: {version}\"\n",
191+
" except:\n",
192+
" pass\n",
131193
" return False, None, f\"⚠️ {command} found but version check failed: {e}\"\n",
132194
"\n",
133195
"def check_python_version():\n",

0 commit comments

Comments
 (0)