pnpm install报错:此系统上禁止运行脚本

依赖安装

报错信息:

pnpm : 无法加载文件 C:\Users\XXX\AppData\Roaming\npm\pnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。

所在位置 行:1 字符: 1

  • pnpm install

  • ~~~~

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

  • FullyQualifiedErrorId : UnauthorizedAccess

解决办法:

PS D:\code\github\langmanus-web> Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

详细根因分析:

命令 `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned` 用于配置 PowerShell 的执行策略,具体含义如下:

**1. 核心功能**
• `-ExecutionPolicy RemoteSigned`:设置执行策略为 RemoteSigned

• 本地脚本:允许无限制运行当前用户编写的脚本(如 `.ps1` 文件)。

• 远程脚本:要求从互联网下载的脚本必须由受信任的发布者进行数字签名,否则会被阻止执行。

• *安全性平衡*:既能满足日常开发需求(运行本地脚本),又避免恶意远程脚本攻击。

• `-Scope CurrentUser`:限定策略生效范围

• 仅影响当前用户:策略修改仅对当前登录的 Windows 用户有效,不会影响其他用户或系统全局设置。

• *非管理员友好*:无需管理员权限即可完成配置。

**2. 技术细节**

• 持久性:策略会被写入注册表 `HKEY_CURRENT_USER\SOFTWARE\Microsoft\PowerShell\1\ShellIds`,在用户注销或重启后依然有效。

• 优先级规则:

• 当多个作用域(如 `LocalMachine` 和 `CurrentUser`)存在冲突策略时,`CurrentUser` 的配置会覆盖全局设置。

• 可通过 `Get-ExecutionPolicy -List` 查看所有作用域的优先级顺序。

• 与组策略的关系:若企业通过组策略强制设置了执行策略(如 `MachinePolicy` 或 `UserPolicy`),此命令可能无效。

**3. 典型应用场景**

  1. 开发者环境配置

• 允许运行本地开发的 PowerShell 脚本(如自动化脚本、CI/CD 工具链)。

• 例如安装 Scoop 包管理器时需临时放宽策略限制。

  1. 安全受限环境

• 在需要限制远程脚本但允许内部脚本的场景中,替代完全开放的 `Unrestricted` 策略。

  1. 多用户隔离

• 同一台计算机上,不同用户可独立配置策略(如管理员保持严格策略,开发者使用宽松策略)。

**4. 注意事项**

• 权限要求:修改 `CurrentUser` 范围无需管理员权限,但首次操作可能需要用户确认。

• 临时会话策略:若需仅对当前 PowerShell 会话生效(不保存到注册表),可用 `-Scope Process`。

• 恢复默认策略:执行 `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Undefined` 可删除用户级策略。

通过此命令,用户可以在保障安全性的前提下灵活控制脚本执行权限。如需验证当前策略,可运行 `Get-ExecutionPolicy -Scope CurrentUser`。

启动

报错

pnpm dev

❌ Invalid environment variables: { NEXT_PUBLIC_API_URL: [ 'Required' ] }

file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29

throw new Error("Invalid environment variables");

^

Error: Invalid environment variables

at onValidationError (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-nextjs@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)

at file:///D:/code/github/langmanus-web/src/env.js:4:20

at ModuleJob.run (node:internal/modules/esm/module_job:271:25)

at async onImport.tracePromise.proto (node:internal/modules/esm/loader:547:26)

at async loadConfig (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\cli\next-dev.js:190:14)

Node.js v22.12.0

ELIFECYCLE  Command failed with exit code 1.

解决方案1:

PS D:\code\github\langmanus-web> echo "NEXT_PUBLIC_API_URL=http://localhost:3000" > .env.local

PS D:\code\github\langmanus-web> pnpm dev

> next dev --turbo

❌ Invalid environment variables: { NEXT_PUBLIC_API_URL: [ 'Required' ] }

file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29

throw new Error("Invalid environment variables");

^

Error: Invalid environment variables

at onValidationError (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-nextjs@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)

at file:///D:/code/github/langmanus-web/src/env.js:4:20

at ModuleJob.run (node:internal/modules/esm/module_job:271:25)

at async onImport.tracePromise.proto (node:internal/modules/esm/loader:547:26)

at async loadConfig (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\cli\next-dev.js:190:14)

Node.js v22.12.0

ELIFECYCLE  Command failed with exit code 1.

解决方案2:

PS D:\code\github\langmanus-web> echo NEXT_PUBLIC_API_URL=http://localhost:3000 > .env

PS D:\code\github\langmanus-web> pnpm dev

> langmanus-web@0.1.0 dev D:\code\github\langmanus-web

> next dev --turbo

❌ Invalid environment variables: { NEXT_PUBLIC_API_URL: [ 'Required' ] }

file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29

throw new Error("Invalid environment variables");

^

at onValidationError (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-nextjs@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)

at file:///D:/code/github/langmanus-web/src/env.js:4:20

at ModuleJob.run (node:internal/modules/esm/module_job:271:25)

at async onImport.tracePromise.proto (node:internal/modules/esm/loader:547:26)

at async loadConfig (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\cli\next-dev.js:190:14)

Node.js v22.12.0

ELIFECYCLE  Command failed with exit code 1.

解决方案3:

PS D:\code\github\langmanus-web> echo "NEXT_PUBLIC_API_URL=http://localhost:3000" > .env.development

PS D:\code\github\langmanus-web> pnpm dev

> next dev --turbo

❌ Invalid environment variables: { NEXT_PUBLIC_API_URL: [ 'Required' ] }

file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29

throw new Error("Invalid environment variables");

^

Error: Invalid environment variables

at onValidationError (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-core@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@t3-oss+env-nextjs@0.10.1_typescript@5.8.2_zod@3.24.2/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)

at file:///D:/code/github/langmanus-web/src/env.js:4:20

at ModuleJob.run (node:internal/modules/esm/module_job:271:25)

at async onImport.tracePromise.proto (node:internal/modules/esm/loader:547:26)

at async loadConfig (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\next@15.2.2_react-dom@18.3.1_react@18.3.1__react@18.3.1\node_modules\next\dist\cli\next-dev.js:190:14)

Node.js v22.12.0

ELIFECYCLE  Command failed with exit code 1.

解决方案4:(有效方案)

PS D:\code\github\langmanus-web> echo NEXT_PUBLIC_API_URL=http://localhost:3000 | Out-File -FilePath .env.local -Encoding UTF8 -NoNewline

PS D:\code\github\langmanus-web> pnpm dev

> langmanus-web@0.1.0 dev D:\code\github\langmanus-web

> next dev --turbo

⚠ Port 3000 is in use, trying 3001 instead.

▲ Next.js 15.2.2 (Turbopack)

· turbo

✓ Starting...

Slow filesystem detected. If D:\code\github\langmanus-web\.next is a network drive, consider moving it to a local folder. If you have an antivirus enabled, consider excluding your project directory.

✓ Ready in 1451ms

命令含义

该命令是一个 PowerShell 脚本命令,用于将环境变量写入 `.env.local` 文件,具体解析如下:


**核心功能解析**

  1. `echo NEXT_PUBLIC_API_URL=http://localhost:3000`

• 通过 `echo` 命令输出字符串 `NEXT_PUBLIC_API_URL=http://localhost:3000`,表示定义环境变量 `NEXT_PUBLIC_API_URL` 并赋值为 `http://localhost:3000`。

• 该环境变量通常用于前端框架(如 Next.js)中配置 API 基础地址。

  1. `| Out-File -FilePath .env.local`

• 使用管道符 `|` 将 `echo` 的输出重定向到 `Out-File` 命令,将内容写入 `.env.local` 文件。

• `.env.local` 是 Next.js 等框架的本地环境变量配置文件,用于存储敏感或开发专用的配置。

  1. `-Encoding UTF8`

• 指定文件编码为 UTF-8,确保特殊字符(如中文)正确保存。

  1. `-NoNewline`

• 禁止在文件末尾自动添加换行符,防止因换行符导致环境变量解析错误。

相关推荐
cpsvps_net15 小时前
美国服务器环境下Windows容器工作负载智能弹性伸缩
windows
甄超锋16 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cpsvps18 小时前
美国服务器环境下Windows容器工作负载基于指标的自动扩缩
windows
网硕互联的小客服21 小时前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
etcix21 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机
许泽宇的技术分享21 小时前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
非凡ghost1 天前
AMS PhotoMaster:全方位提升你的照片编辑体验
windows·学习·信息可视化·软件需求
mortimer1 天前
一次与“顽固”外部程序的艰难交锋:subprocess 调用exe踩坑实录
windows·python·ai编程
gameatp1 天前
从 Windows 到 Linux 服务器的全自动部署教程(免密登录 + 压缩 + 上传 + 启动)
linux·服务器·windows
穷人小水滴1 天前
在 windows 运行 flatpak 应用 (WSL)
linux·windows·ubuntu