0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCodeのターミナル上でnpmのバージョン確認ができない事象

0
Posted at

背景

 VSCode上でターミナルを開き、npm (Node Package Manager) のバージョン確認を試みたところ、下記のエラーが発生。

npm : このシステムではスクリプトの実行が無効になっているため、ファイル C:\Program Files\nodejs\npm.ps1 を読み込むことができません。詳細については、「about_Execution_
Policies(https://go.microsoft.com/fwlink/?LinkID=135170) を参照してください。
発生場所 :1 文字:1
+ npm run dev
+ ~~~
    + CategoryInfo          : セキュリティ エラー: (: ) []PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

 *  ターミナル プロセス "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run dev" が終了コード 1 で終了しました。 
 *  ターミナルはタスクで再利用されます、閉じるには任意のキーを押してください。 

本記事は、上記のエラー解決策および解決に使用するコマンドの詳細について述べる。

解決策

以下のコマンドをPowerShell上で実行することで、エラーを解消可能:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

コマンドの詳細

上記は、PowerShellでスクリプト実行ポリシーを設定するためのコマンドである。

各パラメーターの意味:

Set-ExecutionPolicy:スクリプトの実行ポリシーを設定するコマンド
-Scope CurrentUser:現在のユーザーのみにポリシーを適用。ほかのユーザーやシステム全体には影響なし。
-ExecutionPolicy RemoteSigned:リモートからダウンロードしたスクリプトは署名が必要だが、ローカルで作成したスクリプトは署名不要で実行可能。

このコマンドを使った結果、下記が可能となる。

・ローカルスクリプトの場合、問題なく実行可能
・一方でインターネットからダウンロードしたリモートのスクリプトは署名付きなら実行可能だが、ない場合は実行できないようにブロックしてくれる。

まとめ

 今回は発生したエラーを受け、スクリプトを有効にするため、上記のコマンドを使用。
エラー文の中で「about_Execution_Policies」のリンクがあり、そこに飛ぶと「PowerShell実行ポリシー」内でRemoteSigned以上であれば、スクリプトが実行できるとわかる。
したがって、今回のコマンドを使用し、実行ポリシーを設定した。

参考リンク

・[about_Execution_Policies]:https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.4

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?