remote codespaces 中 node 脚本执行被拒绝,99% 是 powershell 执行策略拦截;需在终端运行 get-executionpolicy -scope currentuser 确认 restricted,再执行 set-executionpolicy remotesigned -scope currentuser,并重启终端生效。

Remote Codespaces 中 Node 脚本执行被拒绝,99% 是 PowerShell 执行策略拦截,不是权限不足
你在 Codespaces 终端里运行 npm start 或直接 node server.js 报错 File start.ps1 cannot be loaded because running scripts is disabled on this system,这不是 Node 没装好,也不是项目路径错了——是 Codespaces 默认终端(PowerShell)沿用了 Windows 的 Restricted 策略,连本地生成的包装脚本(如 node_modules/.bin/npm 实际是 npm.ps1)都会被静默拦截。
为什么 Remote Codespaces 会卡在 PowerShell 策略上
Codespaces 底层是 Linux 容器,但 VSCode 远程终端默认启用了 PowerShell for Linux(通过 powershell-linux),且策略继承自用户配置。即使容器里没装 Windows,PowerShell 仍按 Restricted 模式启动,导致所有 .ps1 封装脚本无法执行。
- 错误现象:终端输出含
cannot be loaded because running scripts is disabled,但node --version正常 - 不是文件权限问题:
chmod +x对.ps1文件完全无效 - 不是 Codespaces 配置缺失:不需要改
devcontainer.json或加sudo - 真正要动的是当前用户的 PowerShell 策略上下文
必须执行的三步修复(无须管理员权限)
在 Codespaces 终端中依次运行:
使用一条命令部署ProbeChain Rydberg测试网代理节点。自动注册为Agent(NodeType=1),免gas,支持macOS/Linux/Windows。触发词:/r
- 检查当前策略:
Get-ExecutionPolicy -Scope CurrentUser—— 若返回Restricted,确认问题 - 放宽策略(仅限当前用户):
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser - 验证生效:
Get-ExecutionPolicy -Scope CurrentUser应输出RemoteSigned
完成后关闭所有终端标签页(Ctrl+Shift+` 关掉再重开),再试 npm run dev。旧终端不会自动刷新策略,重启是必须的。
Node 项目调试时 launch.json 报 “Cannot launch program”
这个错误常被误认为路径不对,实际是调试器启动链被 PowerShell 拦截了——尤其当你在 launch.json 的 program 字段指向一个 shell 脚本(如 ./scripts/start.sh)或 npm script 封装入口时。
- 安全写法:
"program": "${workspaceFolder}/src/index.js"(直接指向 JS 文件) - 避免写法:
"program": "${workspaceFolder}/start.sh"或"program": "npm"(触发 PowerShell 执行封装) - 若必须用脚本启动,改用
console类型任务配合preLaunchTask,绕过调试器直调 - Codespaces 中
node_modules/.bin下的二进制本质是.ps1,所以eslint、ts-node等 CLI 也会失败,同样靠上述策略修复
真正容易被忽略的点:Codespaces 的终端策略是 per-user 的,但如果你用 GitHub 帐号在多个 Codespace 间切换,策略不会自动同步——每个新空间都要单独执行一次 Set-ExecutionPolicy。别指望一次设置永久生效。










