[{"data":1,"prerenderedAt":248},["ShallowReactive",2],{"article-devops\u002Fmac":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":13,"_type":242,"_id":243,"_source":244,"_file":245,"_stem":246,"_extension":247},"\u002Farticles\u002Fdevops\u002Fmac","devops",false,"","macOS 环境变量排查：解决 npm 全局命令 command not found","从 Claude CLI 安装后无法执行出发，梳理 zsh 与 bash 配置文件差异，并给出在 macOS 下修复 PATH 环境变量的实用方案。","2026-03-20",[12],"随笔",{"type":14,"children":15,"toc":235},"root",[16,24,30,76,81,94,107,126,131,187,192],{"type":17,"tag":18,"props":19,"children":21},"element","h2",{"id":20},"先说发现这个坑的起因",[22],{"type":23,"value":20},"text",{"type":17,"tag":25,"props":26,"children":27},"p",{},[28],{"type":23,"value":29},"最近刚考研结束，之前是干前后端开发的，现在想去接触大模型人工智能相关的东西，此处省略一万字学习过程中的心酸历程。今天突然想尝试一下claude code。",{"type":17,"tag":25,"props":31,"children":32},{},[33,35,42,44,50,52,58,60,66,68,74],{"type":23,"value":34},"然后就按照文档",{"type":17,"tag":36,"props":37,"children":39},"code",{"className":38},[],[40],{"type":23,"value":41},"npm install -g @anthropic-ai\u002Fclaude-code",{"type":23,"value":43},"，然后",{"type":17,"tag":36,"props":45,"children":47},{"className":46},[],[48],{"type":23,"value":49},"claude --version",{"type":23,"value":51},"，但！",{"type":17,"tag":36,"props":53,"children":55},{"className":54},[],[56],{"type":23,"value":57},"command not found: claude",{"type":23,"value":59},"，我心思是npm全局安装的路径没有在系统的 ",{"type":17,"tag":36,"props":61,"children":63},{"className":62},[],[64],{"type":23,"value":65},"PATH",{"type":23,"value":67}," 环境变量中。我就去找了",{"type":17,"tag":36,"props":69,"children":71},{"className":70},[],[72],{"type":23,"value":73},".bash_profile",{"type":23,"value":75},"，发现文件里配置了npm的环境变量。",{"type":17,"tag":18,"props":77,"children":79},{"id":78},"原因解读",[80],{"type":23,"value":78},{"type":17,"tag":25,"props":82,"children":83},{},[84,86,92],{"type":23,"value":85},"macOS Catalina (10.15) 及以后版本默认使用Zsh作为终端shell，",{"type":17,"tag":36,"props":87,"children":89},{"className":88},[],[90],{"type":23,"value":91},"~\u002F.zshrc",{"type":23,"value":93},"是默认配置文件。",{"type":17,"tag":25,"props":95,"children":96},{},[97,99,105],{"type":23,"value":98},"旧版macOS和Linux默认使用Bash作为终端，，",{"type":17,"tag":36,"props":100,"children":102},{"className":101},[],[103],{"type":23,"value":104},"~\u002F.bash_profile",{"type":23,"value":106},"是默认配置文件。且macOS不会同时加载两个配置文件。",{"type":17,"tag":25,"props":108,"children":109},{},[110,112,117,119,124],{"type":23,"value":111},"也就是说，现在mac只认",{"type":17,"tag":36,"props":113,"children":115},{"className":114},[],[116],{"type":23,"value":91},{"type":23,"value":118},"的配置，而我环境变量都写在了",{"type":17,"tag":36,"props":120,"children":122},{"className":121},[],[123],{"type":23,"value":104},{"type":23,"value":125},"，不会生效",{"type":17,"tag":18,"props":127,"children":129},{"id":128},"解决办法",[130],{"type":23,"value":128},{"type":17,"tag":132,"props":133,"children":134},"ol",{},[135,153],{"type":17,"tag":136,"props":137,"children":138},"li",{},[139,141,146,148],{"type":23,"value":140},"将",{"type":17,"tag":36,"props":142,"children":144},{"className":143},[],[145],{"type":23,"value":104},{"type":23,"value":147},"中的配置粘贴复制到",{"type":17,"tag":36,"props":149,"children":151},{"className":150},[],[152],{"type":23,"value":91},{"type":17,"tag":136,"props":154,"children":155},{},[156,158,163,165,171,173,178],{"type":23,"value":157},"在",{"type":17,"tag":36,"props":159,"children":161},{"className":160},[],[162],{"type":23,"value":91},{"type":23,"value":164},"中添加如下配置，这样系统会同时加载 ",{"type":17,"tag":36,"props":166,"children":168},{"className":167},[],[169],{"type":23,"value":170},".zshrc",{"type":23,"value":172}," 和 ",{"type":17,"tag":36,"props":174,"children":176},{"className":175},[],[177],{"type":23,"value":73},{"type":17,"tag":179,"props":180,"children":182},"pre",{"code":181},"# 在 zsh 中加载 bash 配置\nif [ -f ~\u002F.bash_profile ]; then\n    source ~\u002F.bash_profile\nfi\n",[183],{"type":17,"tag":36,"props":184,"children":185},{"__ignoreMap":7},[186],{"type":23,"value":181},{"type":17,"tag":18,"props":188,"children":190},{"id":189},"总结此过程学习到的",[191],{"type":23,"value":189},{"type":17,"tag":132,"props":193,"children":194},{},[195,222],{"type":17,"tag":136,"props":196,"children":197},{},[198,204,206,212,214,220],{"type":17,"tag":36,"props":199,"children":201},{"className":200},[],[202],{"type":23,"value":203},"npm config get prefix",{"type":23,"value":205},"： npm（Node Package Manager）命令，用于",{"type":17,"tag":207,"props":208,"children":209},"strong",{},[210],{"type":23,"value":211},"查看当前npm配置中设置的全局安装路径（prefix）",{"type":23,"value":213}," ，是当使用 ",{"type":17,"tag":36,"props":215,"children":217},{"className":216},[],[218],{"type":23,"value":219},"npm install -g 包名",{"type":23,"value":221}," 全局安装包时，包被安装到的位置",{"type":17,"tag":136,"props":223,"children":224},{},[225,227],{"type":23,"value":226},"命令配置环境变量",{"type":17,"tag":179,"props":228,"children":230},{"code":229},"echo 'export PATH=\"\u002Fusr\u002Flocal\u002Flib\u002Fnpm\u002Fbin:$PATH\"' >> ~\u002F.zshrc\nsource ~\u002F.zshrc\n",[231],{"type":17,"tag":36,"props":232,"children":233},{"__ignoreMap":7},[234],{"type":23,"value":229},{"title":7,"searchDepth":236,"depth":236,"links":237},2,[238,239,240,241],{"id":20,"depth":236,"text":20},{"id":78,"depth":236,"text":78},{"id":128,"depth":236,"text":128},{"id":189,"depth":236,"text":189},"markdown","content:articles:devops:mac环境变量.md","content","articles\u002Fdevops\u002Fmac环境变量.md","articles\u002Fdevops\u002Fmac环境变量","md",1779811689932]