Skip to content

orzgithub/fnm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fast Node Manager (fnm) Amount of downloads GitHub Actions workflow status

πŸš€ Fast and simple Node.js version manager, built in Rust

Blazing fast!

Features

🌎 Cross-platform support (macOS, Windows, Linux)

✨ Single file, easy installation, instant startup

πŸš€ Built with speed in mind

πŸ“‚ Works with .node-version and .nvmrc files

Installation

Using a script (macOS/Linux)

For bash, zsh and fish shells, there's an automatic installation script.

First ensure that curl and unzip are already installed on you operating system. Then execute:

curl -fsSL https://fnm.vercel.app/install | bash

Upgrade

On macOS, it is as simple as brew upgrade fnm.

On other operating systems, upgrading fnm is almost the same as installing it. To prevent duplication in your shell config file, pass --skip-shell to the install command:

curl -fsSL https://fnm.vercel.app/install | bash -s -- --skip-shell

Parameters

--install-dir

Set a custom directory for fnm to be installed. The default is $XDG_DATA_HOME/fnm (if $XDG_DATA_HOME is not defined it falls back to $HOME/.local/share/fnm on linux and $HOME/Library/Application Support/fnm on MacOS).

--skip-shell

Skip appending shell specific loader to shell config file, based on the current user shell, defined in $SHELL. e.g. for Bash, $HOME/.bashrc. $HOME/.zshrc for Zsh. For Fish - $HOME/.config/fish/conf.d/fnm.fish

--force-install

macOS installations using the installation script are deprecated in favor of the Homebrew formula, but this forces the script to install using it anyway.

Example:

curl -fsSL https://fnm.vercel.app/install | bash -s -- --install-dir "./.fnm" --skip-shell

Manually

Using Homebrew (macOS/Linux)

brew install fnm

Then, set up your shell for fnm

Using Winget (Windows)

winget install Schniz.fnm

Using Scoop (Windows)

scoop install fnm

Then, set up your shell for fnm

Using Chocolatey (Windows)

choco install fnm

Then, set up your shell for fnm

Using Cargo (Linux/macOS/Windows)

cargo install fnm

Then, set up your shell for fnm

Using a release binary (Linux/macOS/Windows)

Removing

To remove fnm (😒), just delete the .fnm folder in your home directory. You should also edit your shell configuration to remove any references to fnm (ie. read Shell Setup, and do the opposite).

Completions

fnm ships its completions with the binary:

fnm completions --shell <SHELL>

Where <SHELL> can be one of the supported shells:

  • bash
  • zsh
  • fish
  • powershell

Please follow your shell instructions to install them.

Shell Setup

Environment variables need to be setup before you can start using fnm. This is done by evaluating the output of fnm env.

Note

Check out the Configuration section to enable highly recommended features, like automatic version switching.

Adding a .node-version to your project is as simple as:

$ node --version
v14.18.3
$ node --version > .node-version

Check out the following guides for the shell you use:

Bash

Add the following to your .bashrc profile:

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

Zsh

Add the following to your .zshrc profile:

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

Fish shell

Create ~/.config/fish/conf.d/fnm.fish and add this line to it:

fnm env --use-on-cd --shell fish | source

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
  • For Windows location is either:
    • %userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 Powershell 5
    • %userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Powershell 6+
  • To create the profile file you can run this in PowerShell:
    if (-not (Test-Path $profile)) { New-Item $profile -Force }
  • To edit your profile run this in PowerShell:
    Invoke-Item $profile

Windows Command Prompt aka Batch aka WinCMD

fnm is also supported but is not entirely covered. You can set up a startup script for cmd.exe or Windows Terminal and append the following lines:

@echo off
:: for /F will launch a new instance of cmd so we create a guard to prevent an infnite loop
if not defined FNM_AUTORUN_GUARD (
    set "FNM_AUTORUN_GUARD=AutorunGuard"
    FOR /f "tokens=*" %%z IN ('fnm env --use-on-cd') DO CALL %%z
)

Usage with Cmder

Usage is very similar to the normal WinCMD install, apart for a few tweaks to allow being called from the cmder startup script. The example assumes that the CMDER_ROOT environment variable is set to the root directory of your Cmder installation. Then you can do something like this:

  • Make a .cmd file to invoke it
:: %CMDER_ROOT%\bin\fnm_init.cmd
@echo off
FOR /f "tokens=*" %%z IN ('fnm env --use-on-cd') DO CALL %%z
  • Add it to the startup script
:: %CMDER_ROOT%\config\user_profile.cmd
call "%CMDER_ROOT%\bin\fnm_init.cmd"

You can replace %CMDER_ROOT% with any other convenient path too.

Usage with Clink

By default Clink load *.lua under %LOCALAPPDATA%\clink when starting a new WinCMD instence so the startup script should written in lua. So this script will convert the CMD-style environment variable setting statement to lua-style. Write the following script into %LOCALAPPDATA%\clink\fnm.lua (or other path, depending on your clink configuration):

fnm.lua
if (clink.version_encoded or 0) < 10020030 then
	error("fnm requires a newer version of Clink; please upgrade to Clink v1.2.30 or later.")
end

-- ANSI escape codes for colors and styles
local RED   = "\27[31m"
local YELLOW = "\27[33m"
local CYAN = "\27[36m"
local RESET = "\27[0m"
local BOLD  = "\27[1m"
local ITAL  = "\27[3m"

local function prompt_install()
    local handle = io.popen("fnm use --silent-if-unchanged 2>&1")
    local t = handle:read("*a")
    handle:close()

    if not t or t == "" then return end

    -- Check for missing version error
    local version = t:match("error: Requested version ([^%s]+) is not currently installed")
    if version then
        version = tostring(version)
        io.write(
            RED, "Can't find an installed Node version matching ", ITAL, version, RESET, ".\n"
        )
        io.write(
            YELLOW, "Do you want to install it? ", BOLD, "answer", RESET, YELLOW, " [y/N]: ", RESET
        )
        local answer = io.read()
        if answer and answer:lower() == "y" then
            os.execute("fnm use --silent-if-unchanged --install-if-missing")
        end
        return
    end

    -- Success: output contains "Using Node v..."
    local node_version = t:match("Using Node (v%d+%.%d+%.%d+)")
    if node_version then
        io.write("Using Node ", CYAN, node_version, RESET, "\n")
        return
    end

    -- All other cases are errors
    error("fnm use --silent-if-unchanged failed: " .. t)
end

local function parse_fnm_env()
    local handle = io.popen('fnm env --use-on-cd 2>nul')
    if not handle then return nil end
    local out = handle:read("*a")
    handle:close()

    local env = {}
    for line in out:gmatch("[^\r\n]+") do
        -- Matches: set VAR=VALUE  (quotes rarely used, but handle if present)
        local var, value = line:match("^[Ss][Ee][Tt]%s+([^=]+)=(.*)$")
        if var and value then
            value = value:gsub('^"(.*)"$', '%1') -- remove surrounding quotes if present
            env[var:match("^%s*(.-)%s*$")] = value -- trim spaces from var
        end
    end
    return env, out
end

local function check_and_use_fnm()
    -- Check FNM_VERSION_FILE_STRATEGY environment variable
    local fnm_strategy = os.getenv("FNM_VERSION_FILE_STRATEGY")
    
    -- -- Check if we should use recursive strategy
    if fnm_strategy == "recursive" then
        prompt_install()
    else
        -- Check for .nvmrc file
        local nvmrc = io.open(".nvmrc", "r")
        if nvmrc ~= nil then
            io.close(nvmrc)
            prompt_install()
        else
            -- Check for .node-version file
            local node_version = io.open(".node-version", "r")
            if node_version ~= nil then
                io.close(node_version)
                prompt_install()
            end
        end
    end
end

local function setup_fnm()
    local env, raw = parse_fnm_env()
    if not env or not raw then return end

    for var, value in pairs(env) do
        os.setenv(var, value)
    end

    -- doskey replacement
    clink.onbeginedit(check_and_use_fnm)

    -- remove fnm symlinks on exit
    clink.onendedit(function(line)
        if line:match("^exit") then
            local path = os.getenv("FNM_MULTISHELL_PATH")
            if path then
                os.rmdir(path)
            end
        end
    end)
end

setup_fnm()

See the available configuration options for an extended configuration documentation

See the available commands for an extended usage documentation

Contributing

PRs welcome πŸŽ‰

Developing:

# Install Rust
git clone https://github.com/Schniz/fnm.git
cd fnm/
cargo build

Running Binary:

cargo run -- --help # Will behave like `fnm --help`

Running Tests:

cargo test

About

πŸš€ Fast and simple Node.js version manager, built in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 66.4%
  • TypeScript 20.8%
  • JavaScript 8.3%
  • Shell 4.4%
  • Batchfile 0.1%