VS Code编译TypeScript报错:无法加载tsc.ps1 系统禁止运行脚本
TypeScript编译时PowerShell脚本执行禁止报错解决
问题复现场景
在PowerShell终端中执行tsc main.ts编译TypeScript文件时,触发PSSecurityException未授权访问异常,系统提示无法加载npm路径下的tsc.ps1脚本,原因为系统默认PowerShell执行策略禁止运行未授信脚本。
完整报错输出:
tsc : File C:\Users\emanull\AppData\Roaming\npm\tsc.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see PS C:\Users\emanull\Desktop\backend> tsc main.ts tsc : File C:\Users\emanull\AppData\Roaming\npm\tsc.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 + tsc main.ts + ~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
可行解决方法
- 临时会话放行(无永久修改,关闭终端即失效)
在当前报错的PowerShell窗口直接执行以下命令,仅对当前终端窗口生效,不会修改系统全局配置:
弹出确认提示时输入Set-ExecutionPolicy RemoteSigned -Scope ProcessY回车,之后重新执行tsc main.ts即可正常编译。 - 当前用户永久生效(推荐,安全性有保障)
无需管理员权限,执行以下命令为当前登录用户配置执行策略:允许运行本地自己创建/安装的未签名脚本,从网络下载的脚本必须持有可信签名才能运行,不会带来额外安全风险:
确认提示输入Set-ExecutionPolicy RemoteSigned -Scope CurrentUserY回车后,所有新开的PowerShell终端都可以直接运行tsc、node等全局npm命令,不会再触发该报错。 - 零配置绕过(不修改任何系统策略)
PowerShell默认优先匹配.ps1后缀的脚本执行,执行命令时手动指定调用cmd版本的tsc程序即可绕过PowerShell的脚本检查,不需要改任何配置:tsc.cmd main.ts
内容的提问来源于stack exchange,提问作者emanuel
相关产品推荐
相关产品推荐

