使用PowerShell命令来运行express命令

简述

Node.js命令提示符(以下简称NCP)可以通过运行.bat文件来启用express命令,但在PowerShell中无法正常工作。由于PowerShell更易于使用,我尝试在这里使用它。虽然我只是在进行环境变量的设置,所以并没有什么大不了的。

前提 tí) – precondition

确保已配置使得可以从PowerShell执行ps1文件。
[参考]
http://www.atmarkit.co.jp/ait/articles/0805/16/news139.html
https://technet.microsoft.com/ja-jp/scriptcenter/powershell_owner05.aspx

重要步骤

创建PowerShell快捷方式

在任意位置创建指向PowerShell.exe的快捷方式,并将快捷方式属性设置如下:
设置在PowerShell启动时执行nodejs文件夹内的nodevars.ps1。
-noexit用于确保在脚本执行后PowerShell不会退出。

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit &'C:\Program Files\nodejs\nodevars.ps1'

创建一个ps1脚本

这个脚本用于设置环境变量。
它将脚本位置和express的APPDATA/npm设置为环境变量。
将该脚本以nodevars.ps1的名称保存在指定的位置(在这里是C:\Program Files\nodejs),需要管理员权限。

$scriptPath = $MyInvocation.MyCommand.Path
$scriptPath = Split-Path $scriptPath -Parent 
$env:path = $env:path + ";" + $env:APPDATA + "/npm;" + $scriptPath

以上。