35

I am trying to compile a project on Windows 10 in Visual Studio Code, my settings are the following:

1) npm version 6.12

2) Node.js version 12.13

3) Angular CLI: 8.3.19

The problem I have is as soon as I try to run ng serve I receive the following input : ng : File C:\Users\name\AppData\Roaming\npm\ng.ps1 cannot be loaded because running scripts is disabled on this system. Followed by For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

What I tried so far

1) I went on the website advised and found that Set-ExecutionPolicy -ExecutionPolicy RemoteSigned should be the command I have to enter in the shell. Therefore what I did was:

PS C:\Users\raggi\OneDrive\Desktop\pitalkdashboard> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

But that led to the following error:

Set-ExecutionPolicy : Access to the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".

And that is exactly what I did, so in order:

PS C:\Users\raggi\OneDrive\Desktop\pitalkdashboard> Set-ExecutionPolicy -Scope CurrentUser

Below was prompted by the Shell, I only had to insert RemoteSigned:

cmdlet Set-ExecutionPolicy at command pipeline position 1

Supply values for the following parameters:

ExecutionPolicy: RemoteSigned

After that I tried ng serve to launch the whole project but received a different error, for which I am putting below a print screen as it is too long:

error

Don't have any more ideas on going on on this route.

2) As advised here and also here

I tried to access and run as administrator as shown in the print screen below:

admin

but the only things I visualize on the shell is the following below (however the shell disappears right away and I only had the time to take the print screen)

shell2

3) I dug more into the problem and found some additional insights such as this one and also this additional source. However the only thing that the post was pointing out was, again, to use Set-ExecutionPolicy -ExecutionPolicy RemoteSigned which I tried at point 1) above.

4) After a bit more research I came across this one but unfortunately no useful information I used.

the code

Below is what I have in my package.json if that is useful:

{
  "name": "pi-talk-dashboard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "^1.0.0-beta.5",
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "chart.js": "^2.7.3",
    "core-js": "^2.5.4",
    "electron": "^7.1.0",
    "express": "^4.17.1",
    "moment": "^2.22.2",
    "ng2-charts": "^1.6.0",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.17",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.4.1",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

EDITS

After running the -ExecutionPolicy Unrestricted as admin the following script opened in notepad but I am not sure about its use. I am attaching both print screen and code of the file that opened:

admin

code is below:

#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
  # Fix case when both the Windows and Linux builds of Node
  # are installed in the same directory
  $exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
  & "$basedir/node$exe"  "$basedir/node_modules/@angular/cli/bin/ng" $args
  $ret=$LASTEXITCODE
} else {
  & "node$exe"  "$basedir/node_modules/@angular/cli/bin/ng" $args
  $ret=$LASTEXITCODE
}
exit $ret

EDIT 2

After running on PowerShell -ExecutionPolicy Unrestricted nothing changed. But an error came out. Data path ".builders['app-shell']" should have required property 'class'. See print screen below

powershell

I am running out of ideas on how to run this project and on how to solve this ng serve problem, any insights or guidance in another direction will be useful to solve this issue.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • This should be related to a recent feature of npm I found to be problematic: . Quick and dirty ways to fix are to add `cmd` before the command (eg `cmd ng serve`) or `.cmd` after the command(eg `ng.cmd serve` ) or even just delete all the ps1 files in %appdata%\npm to force powershell to use the cmd file. – Cerlancism Nov 15 '19 at 18:46

13 Answers13

108

You have to set your execution policy to Unrestricted not RemoteSigned. According to Set-ExecutionPolicy:

RemoteSigned. Requires that all scripts and configuration files downloaded from the Internet are signed by a trusted publisher. The default execution policy for Windows server computers.

Since 99% of all PowerShell scripts are unsigned, 99% of the time you will not be able to run them under this policy.

  1. Open PowerShell as Adminstrator.
  2. Run:

.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • Hi HAL9256, thanks for reading my question. As explained at point 2) I am not able to access to `ng` script using the `admin` privileges because the shell disappears right away. Is there an additional way to access to the PowerShell as Admin directly on `Visual Studio` embedded Shell? – Emanuele Nov 15 '19 at 16:33
  • (Just to clarify) You only need to run PowerShell as Administrator to set the execution policy to `Unrestricted` **once** to set the policy *globally*. (have you done this?) everything else doesn't need to run as administrator – HAL9256 Nov 15 '19 at 16:56
  • The shell will quickly disappear when there is a terminating error (i.e. it runs, errors out and exits). The "best" way is to open a new PowerShell console, navigate to your directory, and run your scripts from there. That way any errors are displayed in the console. After setting the execution policy, what errors are you getting? (please edit the original answer with a section on any new errors) – HAL9256 Nov 15 '19 at 17:01
  • I think I am almost there. I am running the shell as admin and went to the folder where file is located: `C:\Users\name\AppData\Roaming\npm>ls` – Emanuele Nov 15 '19 at 17:36
  • and I see the following files: `ng` `ng.cmd` `ng.ps1` `node_modules` – Emanuele Nov 15 '19 at 17:37
  • Now from here `C:\Users\name\AppData\Roaming\npm>` how do I set the property for the specific file `ng.ps1` to be `Unrestricted`? – Emanuele Nov 15 '19 at 17:38
  • Ok I just run the following command `C:\Users\name\AppData\Roaming\npm>ng.ps1 -ExecutionPolicy Unrestricted` and a file opened up. I edited the question under **EDITS**. I am not sure on how to move forward. – Emanuele Nov 15 '19 at 17:58
  • No. Follow the 2 steps in my answer exactly first. Don't do anything else, or run any other script. This is a PowerShell setting. NOT a script setting. – HAL9256 Nov 15 '19 at 18:36
  • Once the PowerShell setting is set, then you can navigate to your code and run `ng serve` – HAL9256 Nov 15 '19 at 18:38
  • Ok great, last question before I run it: as I open the PowerShell as admin it brings me here `C:\WINDOWS\system32>` , so should I run the execution policy here instead of going to the path of the file? – Emanuele Nov 15 '19 at 18:58
  • Ok I tried but nothing changed. I posted **EDITS 2** on the question. That triggered for an error `Data path ".builders['app-shell']" should have required property 'class'.` Any idea on that? – Emanuele Nov 15 '19 at 20:25
  • The issue now is not with running `ng serve` the issue is now with your code. i.e. if you build your app with `ng build` you will see the same error. I would guess that the problem is in the `shell` class. You will have to paste the code for `shell.component.ts` – HAL9256 Nov 15 '19 at 20:44
  • This solution solved my problem. Thank you a lot :) – Geshe Feb 23 '21 at 12:21
  • Is there any permanent solution for this? As every time I started my computer and start ng serve in powershell I need to run this "Set-ExecutionPolicy -ExecutionPolicy Unrestricted". Before this is not happened to me in my computer actually it is not happened in my colleague's computer as well. As I setup my computer again with windows 10 it start to get this error. – Sandip Vora Mar 06 '21 at 08:31
  • The current "solution" is to run PowerShell as Administrator to set the global execution policy to `Unrestricted`. If you can't do that, you have to manually set it every time you start PowerShell. (aside) In general this is not really "recommended" as it is turning off one security mechanism, but not setting it for a dev renders `ng` virtually unusable in VS Code. The "real" permanent solution is for npm, et al., to sign all PowerShell scripts with a publicly trusted code signing certificate (don't bypass security, build it with security in mind). This is very hard to do on a public project. – HAL9256 Mar 09 '21 at 00:45
17
  • Issue

I face the same issue and after some research I found simply bypass the restriction and launch your app thru ng server.

  • IN TERMINAL you can type below:

PS C:\Users\\\ClientApp> Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

PS C:\Users\\\ClientApp> ng serve

  • Rerun your app :

http://localhost:4200/

Ashutosh Soni
  • 191
  • 1
  • 4
11

Try using "npx ng" instead of "ng". I get the same permissions error if I try using any "ng" command, including "ng new". Using this npm package runner solves it for me.

Rachit Sharma
  • 111
  • 1
  • 3
9

Open Powershell as Admin and execute the below command:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
David Buck
  • 3,752
  • 35
  • 31
  • 35
Mumin Quadri
  • 147
  • 1
  • 1
7

Run this command on your project folder

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Good luck

2

I was trying to run the electron-packager and ran into this problem

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

Fixed the problem.

Adrian Smith
  • 1,013
  • 1
  • 13
  • 21
2

Open the file as mentioned in the error and just delete the ng.ps1 file and it will work fine..

In my case it was nodemon.ps1. I deleted it and server started.

Vinay
  • 2,272
  • 4
  • 19
  • 34
2

Run below command in powershell.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

If everything is okay , you will see like below screen.

enter image description here

Brajesh
  • 1,515
  • 13
  • 18
1

I have Installed angular 12, Even I got the error like below, when I run the application using ng serve

  • CategoryInfo : SecurityError: (:) [], PSSecurityException
  • FullyQualifiedErrorId : UnauthorizedAccess

Below is the command I used to resolve the UnauthorizedAccess issue

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
Sreehari Ballampalli
  • 3,404
  • 3
  • 12
  • 19
1

I had the same issue when running Angular 12 application. Running the following script by opening Windows PowerShell as administrator mode was solved the issue.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
  • I faced the above issue while adding material to my angular project ng add @angular/material The above command worked for me. I opened power shell in administrator mode. – Dipan Mandal Jan 22 '23 at 18:10
0

adding bin folder path to environmental variable path , will work

Aditya
  • 1
  • 1
0

Set an environmental variable path, but not working for my case. then used power shell (as administrator mode) run the below line

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

now it is working. thanks

0

Solved in 3 steps

  1. First, open the command prompt and run this command.

set-ExecutionPolicy RemoteSigned -Scope CurrentUser

  1. Now run the second command

Get-ExecutionPolicy

  1. To view policies, run this command

Get-ExecutionPolicy -list

then you will see

Scope ExecutionPolicy


MachinePolicy Undefined

UserPolicy Undefined

Process Undefined

CurrentUser RemoteSigned

LocalMachine Undefined

CurrentUser RemoteSigned

LocalMachine Undefined

now run command ng --version or ng serve

Saurabh
  • 472
  • 6
  • 6