This error occurs because PowerShell has a security policy that restricts the execution of certain scripts, including the npm.ps1 script, which is part of the Node.js installation. By default, the PowerShell execution policy may be set to "Restricted" or "AllSigned," preventing these types of scripts from running.
Here's how to solve it:
Solution 1: Change PowerShell's Execution Policy Temporarily You can bypass this policy temporarily by changing the execution policy for the current session only:
Open PowerShell as Administrator. Run this command: powershell
Copy code "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass"
Now, run the npm --version command again to check if it works. This method only changes the execution policy for the current session, so you won’t need to modify the system policy permanently.
Solution 2: Change PowerShell's Execution Policy Permanently If you prefer a more permanent solution, you can change the execution policy on your system:
Open PowerShell as Administrator. Run this command: powershell
Copy code "Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned"
Confirm by typing Y (Yes) if prompted. This change will allow locally created scripts to run, while still protecting against untrusted remote scripts.
After following either of these solutions, try running npm --version in PowerShell again.