You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行npm install npm@latest -g更新后无法运行npm命令求助

Fixing npm SyntaxError After Updating to Latest Version

Hey there, that error you're hitting is a dead giveaway that the latest npm version you installed needs a newer Node.js than what you’re running right now. Block-scoped declarations (let/const) outside strict mode weren’t fully supported in Node.js until version 6.x, and modern npm releases (like v9+) require Node.js 14.17 or higher. Since you were on npm 5.0.3 (which pairs with older Node versions), the new npm can’t run on your outdated Node setup.

Here are two solid solutions to get npm working again:

Option 1: Roll Back to an npm Version Compatible With Your Current Node.js

If you don’t want to update Node.js right now, you can revert to an npm version that plays nice with your existing setup (like your original 5.0.3):

  1. First, clean up the broken npm installation:

    • On macOS/Linux: Delete the global npm folders with these commands:
      sudo rm -rf /usr/local/lib/node_modules/npm
      rm -rf ~/.npm
      
    • On Windows: Open File Explorer and delete these folders:
      • %AppData%\npm
      • %AppData%\npm-cache
  2. Reinstall the compatible npm version:

    • macOS/Linux: Use the official npm install script to target version 5.0.3:
      curl -L https://www.npmjs.com/install.sh | sh -s -- --version 5.0.3
      
    • Windows: The easiest way is to download and reinstall the Node.js version that originally came with npm 5.0.3 (Node.js 8.x or similar) — the installer will automatically set up the matching npm version for you.
  3. Verify it worked: Run npm --version to confirm you’re back on 5.0.3, then test commands like npm install to make sure they run without errors.

Option 2: Update Node.js to Support the Latest npm

If you want to keep using the latest npm, you’ll need to upgrade your Node.js to a version that meets its requirements (Node.js 14.17+ for npm v9+):

  1. Use Node Version Manager (nvm) for easy version management:

    • macOS/Linux: Install nvm with this command:
      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
      
      (Close and reopen your terminal after installation for changes to take effect.)
    • Windows: Download and install the Windows version of nvm, following its official setup instructions.
  2. Install a compatible Node.js version:

    nvm install 18  # Node.js 18 is LTS and fully supports modern npm
    
  3. Switch to the new Node version:

    nvm use 18
    
  4. Reinstall the latest npm (optional — Node 18 comes with a recent npm out of the box):

    npm install npm@latest -g
    
  5. Verify everything works: Run node --version to confirm you’re on Node 18+, then npm --version to check the npm version. Test npm install to ensure no more errors pop up.


内容的提问来源于stack exchange,提问作者Stephanie Cray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:58:24