fnm安装nodejs后每次都要执行fnm env命令

439 阅读1分钟

PowerShell

Add the following to the end of your profile file:

fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression
  • For macOS/Linux, the profile is located at ~/.config/powershell/Microsoft.PowerShell_profile.ps1

  • On Windows to edit your profile you can run this in a PowerShell

    notepad $profile
    

以上是官方文档的操作流程,但是普通用户可能遇到执行失败的问题,比如无法找到profile,无法执行脚本等。如何解决。 Here is how to set up the PowerShell profile:

  1. First, check that your profile does not exist. in PowerShell, run: notepad $profile.

    If Notepad cannot find the path, continue below, otherwise, jump to step 3.

  2. Open PowerShell using admin access and paste the code:

     ps:> if (!(Test-Path -Path $PROFILE)) {
               New-Item -ItemType File -Path $PROFILE -Force
          }
    

    This basically create the profile path. (script from Microsoft).

    Restart PowerShell.

  3. Now we're going to make sure PowerShell is able to run scripts. Open it and run:

    ps:> Get-ExecutionPolicy
    
    # If it says Restricted, then it won't run node, or it's associated env variables.
    
  4. To change it, open PowerShell in admin access, and use this code to change the restriction for PowerShell execution policies:

ps:> Set-ExecutionPolicy -ExecutionPolicy Unrestricted

# Accept the change and restart PowerShell again.

This is my preferred option, since I have not experimented with the other options--found in the link in this bullet point.

  1. Now, you should be able to open the Profile path by running:
ps:> notepad $PROFILE

# append the script from the `fnm` documentation into the file
ps:> fnm env --use-on-cd | Out-String | Invoke-Expression
  1. Now you should be able to just type node --version and it should work with no issue.