How to Fix “Command Not Found” Errors After Globally Installing a CLI Tool on Windows

    A practical guide to diagnosing CLI tools that cannot be recognized after global installation with npm on Windows. It explains how to verify the global installation path, update the PATH environment variable, refresh terminal sessions, reinstall corrupted packages, and check PowerShell permissions and execution policies.

    When working with front-end or Node.js projects, developers often install command-line tools globally through a package manager. However, after a seemingly successful installation, running the tool in PowerShell or Command Prompt may produce an error such as:

    1. The term 'xxx' is not recognized as the name of a cmdlet, function, script file, or operable program.

    This article explains why this happens and provides a systematic approach to diagnosing and fixing the problem.

    1. Understanding the Error and Its Root Causes

    1.1 Reproducing the Problem

    A typical scenario looks like this:

    1. npm install -g <cli-tool-name>

    The terminal reports that the installation completed successfully. However, when you verify the installation with:

    1. <cli-tool-name> --version

    Windows reports that the command cannot be recognized.

    1.2 Common Causes

    In most cases, the terminal cannot find the executable file or script associated with the command in the current directory or in any directory listed in the PATH environment variable. Common causes include:

    1. The global installation directory is missing from PATH
      When npm installs a package globally, it places the package and its executable scripts in a global directory, such as the Node.js prefix directory or its corresponding executable directory. If that directory is not included in PATH, Windows cannot locate the command.

    2. The installation did not complete correctly
      Network instability, a damaged package-manager cache, missing dependencies, or permission problems may prevent the executable script from being written correctly.

    3. The terminal session has stale environment variables
      If PATH was changed before the installation but the current terminal was not restarted, the session may still be using the old environment-variable configuration.

    4. PowerShell execution-policy restrictions
      PowerShell may block certain scripts based on its execution policy. Even when the installation path is correct, a restricted policy can prevent a command from running.

    2. A Systematic Troubleshooting Process

    2.1 Verify and Configure the Global Installation Path

    Checking the global installation path is often the most important step.

    Step 1: Find the global installation directory

    For npm, run:

    1. npm config get prefix

    The command may return a path similar to:

    1. C:\Users\Username\AppData\Roaming\npm

    Step 2: Check the PATH environment variable

    On Windows, open the environment-variable settings:

    1. Right-click This PC and select Properties.
    2. Open Advanced system settings.
    3. Click Environment Variables.
    4. Locate the Path variable under either User variables or System variables.
    5. Check whether the path returned by npm config get prefix is included.

    Step 3: Add the path if necessary

    If the global npm path is not listed, add it as a new entry and save the changes.

    After modifying environment variables, close all open terminal windows and start a new PowerShell or Command Prompt session. Existing terminal sessions usually retain the environment variables that were loaded when they started.

    2.2 Clear the Cache and Reinstall the CLI Tool

    If the PATH configuration is correct but the command still cannot be found, the installation itself may be incomplete or corrupted.

    Step 1: Clear the npm cache

    Run:

    1. npm cache clean --force

    This removes the local npm cache and helps avoid problems caused by damaged cached packages.

    Step 2: Reinstall the package globally

    Run the installation command again:

    1. npm install -g <cli-tool-name>

    Make sure the network connection is stable and review the terminal output carefully. Pay attention to dependency errors, permission warnings, or other messages indicating that the installation did not finish normally.

    Step 3: Verify the installation

    After the installation completes, run:

    1. <cli-tool-name> --version

    If the terminal returns a version such as:

    1. 2.1.148

    the executable script was written correctly and Windows can now locate it.

    2.3 Check Terminal Permissions and PowerShell Execution Policies

    In some environments, a global installation may need to write files to a protected system directory. A terminal without sufficient permissions may cause the installation to fail or behave unexpectedly.

    Run the terminal as an administrator

    Close the current terminal, open PowerShell or Command Prompt by selecting Run as administrator, and try the global installation again.

    Check the PowerShell execution policy

    If the problem occurs specifically in PowerShell, check the current policy:

    1. Get-ExecutionPolicy

    If the result is Restricted, local scripts may be blocked. Using an administrator PowerShell session, the policy can be changed with:

    1. Set-ExecutionPolicy RemoteSigned

    This relaxes restrictions on local scripts while maintaining signing requirements for scripts downloaded from the internet.

    3. Long-Term Prevention and Best Practices

    3.1 Manage the Global Installation Path Consistently

    When setting up a development environment, choose a consistent global package path and make sure it is included in the system PATH. Avoid changing the npm prefix frequently. A stable configuration makes it easier for Windows to locate all globally installed CLI tools.

    3.2 Be Careful When Using Version Managers

    Node.js developers often use tools such as nvm to switch between Node.js versions. Switching versions can change the global installation path. After changing versions, verify that the global path for the active version is correctly mapped in PATH. You may also need to reinstall the CLI tools required by that version.

    3.3 Review Installation Output

    Package managers usually provide detailed installation logs. Get into the habit of reviewing the output instead of assuming that an apparent success message means everything was installed correctly. Warnings marked WARN or errors marked ERR may explain why a command later fails.

    Conclusion

    The Windows error indicating that a command is not recognized is usually caused by a disconnect between the CLI executable’s installation directory and the directories listed in the PATH environment variable. A reliable troubleshooting process is to verify the global npm path, update PATH, restart the terminal, clear the cache and reinstall the package when necessary, and check terminal permissions or PowerShell execution policies.

    This approach is not limited to one CLI tool. It applies broadly to development tools that depend on command-line access and can help restore a stable, efficient development workflow.