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/@[email protected][email protected][email protected]/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/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/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\[email protected][email protected][email protected][email protected]\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\[email protected][email protected][email protected][email protected]\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/@[email protected][email protected][email protected]/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/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/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\[email protected][email protected][email protected][email protected]\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\[email protected][email protected][email protected][email protected]\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

> [email protected] 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/@[email protected][email protected][email protected]/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/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/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\[email protected][email protected][email protected][email protected]\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\[email protected][email protected][email protected][email protected]\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/@[email protected][email protected][email protected]/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/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29:15)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:35:16)

at createEnv (file:///D:/code/github/langmanus-web/node_modules/.pnpm/@[email protected][email protected][email protected]/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\[email protected][email protected][email protected][email protected]\node_modules\next\dist\server\config.js:964:36)

at async Module.nextDev (D:\code\github\langmanus-web\node_modules\.pnpm\[email protected][email protected][email protected][email protected]\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

> [email protected] 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`

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

相关推荐
依旧阳光的老码农几秒前
Windows下使用 VS Code + g++ 开发 Qt GUI 项目的完整指南
开发语言·windows·qt
zhishishe7 小时前
2025 年免费 Word 转 PDF 转换器有哪些?
android·windows·pdf·电脑·word
zhishishe15 小时前
工具指南:免费将 PDF 转换为 Word 的 10 个工具
android·windows·pdf·word
h397416 小时前
MFC文件-写MP4
c++·windows·音视频·mfc
xiaowu08016 小时前
C# 使用Windows API实现键盘钩子的类
windows·c#·计算机外设
love530love17 小时前
PyCharm 链接 Podman Desktop 的 podman-machine-default Linux 虚拟环境
linux·运维·windows·pycharm·podman
人猿泰飞20 小时前
【AI训练环境搭建】在Windows11上搭建WSL2+Ubuntu22.04+Tensorflow+GPU机器学习训练环境
windows·ubuntu·机器学习·wsl·gpu训练
刘梦凡呀1 天前
C# .NET Core 批量下载文件
windows·c#·.netcore