Using Winget (Windows)

1
winget install Schniz.fnm

PowerShell

将以下命令添加至配置文件末尾。

  • For Windows location is either:
    • %userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 Powershell 5
    • %userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Powershell 6+
1
fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

在 Windows 中,也可以运行notepad $profile命令打开 PowerShell 配置文件,如果提示找不到文件路径,就在新建文件夹中添加上述命令,然后保存文件并命名为Microsoft.PowerShell_profile.ps1即可,注意需要保证父级文件夹名称为WindowsPowerShell

Bash

Add the following to your .bashrc profile:

1
eval "$(fnm env --use-on-cd --shell bash)"

执行后 重新打开powershell出现 无法加载文件 C:\Users\amarlyx\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁止运行脚本异常时

Windows PowerShell 的执行策略限制了脚本的运行。您可以通过以下步骤来解决此问题:

步骤 1: 以管理员身份打开 PowerShell

  1. 在开始菜单中搜索“PowerShell”。
  2. 右键单击 Windows PowerShell,然后选择 以管理员身份运行

步骤 2: 修改执行策略

  1. 在 PowerShell 中输入以下命令,允许脚本的执行:

    1
    Set-ExecutionPolicy RemoteSigned

    这将允许本地脚本执行,但需要远程脚本具有有效的签名。

  2. 如果您希望允许任何脚本执行(包括没有签名的远程脚本),您可以使用:

    1
    Set-ExecutionPolicy Unrestricted

步骤 3: 确认执行策略

执行策略修改后,PowerShell 会要求您确认操作。输入 Y 然后按 Enter 键。

步骤 4: 重新启动 PowerShell

修改完成后,关闭 PowerShell 并重新启动它。