依賴安裝
報錯信息:
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 包管理器時需臨時放寬策略限制。
2. 安全受限環境 ?
? ?? 在需要限制遠程腳本但允許內部腳本的場景中,替代完全開放的 `Unrestricted` 策略。
3. 多用戶隔離 ?
? ?? 同一臺計算機上,不同用戶可獨立配置策略(如管理員保持嚴格策略,開發者使用寬松策略)。
**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)
? ?- Local: ? ? ? ?http://localhost:3001
? ?- Network: ? ? ?http://10.239.20.117:3001
? ?- Environments: .env.local, .env.development, .env
? ?- Experiments (use with caution):
? ? ?· 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 基礎地址。
2. `| Out-File -FilePath .env.local` ?
? ?? 使用管道符 `|` 將 `echo` 的輸出重定向到 `Out-File` 命令,將內容寫入 `.env.local` 文件。
? ?? `.env.local` 是 Next.js 等框架的本地環境變量配置文件,用于存儲敏感或開發專用的配置。
3. `-Encoding UTF8` ?
? ?? 指定文件編碼為 UTF-8,確保特殊字符(如中文)正確保存。
4. `-NoNewline` ?
? ?? 禁止在文件末尾自動添加換行符,防止因換行符導致環境變量解析錯誤。
?