Nativescript-cli: Unable to execute any CLI command in PowerShell

Created on 18 Sep 2019  路  4Comments  路  Source: NativeScript/nativescript-cli

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 6.1.1
  • Cross-platform modules: Not applicable
  • Android Runtime: Not applicable
  • iOS Runtime: Not applicable
  • Plugin(s): Not applicable

Describe the bug
Whenever I try to run any CLI command (even tns --version) in PowerShell, I receive an error:

tns.ps1 : File C:\Users\vladimirov\AppData\Roaming\npm\tns.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at 
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\tns.ps1
+ ~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

To Reproduce

  1. Install npm 6.11.3 (not sure which version is causing the problem): npm i -g npm
  2. Install NativeScript CLI globally: npm i -g nativescript
  3. Try tns --version in PowerShell on Windows

Expected behavior
The commands should work.

Additional context
Everything works fine in CMD.

windows

Most helpful comment

In newer npm versions, when a global package is installed, npm creates three different executables for it:

  1. shell executable - used on Linux, macOS, etc.
  2. .cmd executable - used by default on Windows in Command-Prompts
  3. .ps1 executable - used by default in PowerShell.

With previous versions the .ps1 executable was not created by npm and when trying to run tns in PowerShell, the .cmd executable was used.

Due to this change, the tns (and any other newly installed global packages) cannot be executed by default on Windows as the default Execution Policy on Windows is set to Restricted, i.e. it does not allow execution of any scripts.

To change this, you can set different execution policy on your machine as described here:
For example, in case you want to change the execution policy only for the current PowerShell process (i.e. do not persist the value between sessions), you can execute:
```PowerShell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
````
NOTE: This will prompt you to confirm the change - select "Yes"

After that you should be able to execute tns commands.

All 4 comments

In newer npm versions, when a global package is installed, npm creates three different executables for it:

  1. shell executable - used on Linux, macOS, etc.
  2. .cmd executable - used by default on Windows in Command-Prompts
  3. .ps1 executable - used by default in PowerShell.

With previous versions the .ps1 executable was not created by npm and when trying to run tns in PowerShell, the .cmd executable was used.

Due to this change, the tns (and any other newly installed global packages) cannot be executed by default on Windows as the default Execution Policy on Windows is set to Restricted, i.e. it does not allow execution of any scripts.

To change this, you can set different execution policy on your machine as described here:
For example, in case you want to change the execution policy only for the current PowerShell process (i.e. do not persist the value between sessions), you can execute:
```PowerShell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
````
NOTE: This will prompt you to confirm the change - select "Yes"

After that you should be able to execute tns commands.

Hello,
I have exactly the same problem.
Unfortunately, the given link no longer works.
Is there a solution for the problem or a good workaround?

Thanks for your help

I can't vouch for any downstream impact this may have, but I just went into my "C:\Users{USER}\AppData\Roamingnpm\" folder and removed the tns.ps1 file and was able to run tns commands again. Still only a temporary solution as an upgrade to the NS cli results in the file being recreated.

While using vscode trying to run react native, I kept getting the same error non of the command lines worked. Deleting the folder from the NPM files was the only solution that worked for me.

Was this page helpful?
0 / 5 - 0 ratings