Skip to content

Instantly share code, notes, and snippets.

@Grawl
Last active August 18, 2024 22:39
Show Gist options
  • Save Grawl/461c7c1acfcf7e2ecbf99ce9fed40c31 to your computer and use it in GitHub Desktop.
Save Grawl/461c7c1acfcf7e2ecbf99ce9fed40c31 to your computer and use it in GitHub Desktop.
migrate from nvm to asdf

TL;DR

With ASDF, you can manage version of Node, Yarn, PNPM, PHP, Python, and more than 400 other tools, languages and binaries.

  1. Uninstall nvm

  2. Install asdf

  3. Install nodejs plugin for asdf

    Add legacy_version_file = yes to ~/.asdfrc file

  4. Install gnupg

  5. Install global Node version using asdf install nodejs lts

    Set is as global default using asdf global nodejs lts

  6. Open your project folder and install Node version mentioned in .nvmrc using asdf install

    When you open this folder again, asdf will set this version automatically

Detailed manual

1. Uninstall nvm

No need to remove all installed Node versions one-by-one. They all are placed in ~/.nvm folder, so just remove it:

$ cd ~
$ rm -rf .nvm .nvmrc

Then, remove NVM initiation scripts from your .bashrc / .zshrc / etc.

Find this line:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

You may have a Deeper Shell Integration script to install Node version as you cd into a folder.

To ensure NVM is successfully uninstalled, just find any nvm words into your run configurations.

To test NVM is properly uninstalled, just open a new terminal session and you should do not have any errors or warnings related to NVM.

Also, Node is not in your $PATH now, and you should see following in your command line:

$ node -v
command not found: node

2. Install asdf

Open "Download asdf" section of "Getting Started" page on asdf-vm.com and copy installation line. When this instruction was written, there was the following:

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1

You may have a different version since asdf will be updated in future.

Then, add initiating script to your .bashrc / .zshrc:

. $HOME/.asdf/asdf.sh

3. Install nodejs plugin for asdf

asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git

To allow using old .nvmrc files, add legacy_version_file = yes to ~/.asdfrc file

4. To start downloading Node you have to install gnupg

For macOS, simplest way is to install it using Homebrew, like this:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(it takes some time to install Homebrew)
$ brew install gnupg
(it takes some time too)

For Ubuntu, you already have all requirements to use asdf.

5. Install global Node version

asdf syntax to install a tool versions is asdf install <plugin name> <version>, so run following:

asdf install nodejs lts

Then, set is as global default:

asdf global nodejs lts

After this, you can use Node again

$ node -v
v16.13.1

6. Install Node for you project

Open your project folder and run:

asdf install

Ready to go.

When you open this folder again, asdf will set this version automatically.

Further Reading

ASDF has a lot of plugins to install and manage versions of many tools, check out Central plugin repository for asdf

Probably you want to use one of this plugins after installing Node:

@Grawl
Copy link
Author

Grawl commented Apr 4, 2022

@gianpaj
Copy link

gianpaj commented Apr 24, 2022

To uninstall, I would also suggest doing it version by version,
as per https://faizmokhtar.com/posts/migrating-from-nvm-to-asdf/

# list out all the nodes version I'm using
$~ nvm ls
# uninstall nodejs
$~ nvm uninstall <NODEJS_VERSION> 
# uninstall nvm with brew (since I install it using Brew)
$~ brew uninstall nvm
# delete related dotfiles and directory
$~ rm -rf ~/.nvm/ ~/.nvmrc

and also, checking for each version which global packages you have installed that you might still need

npm list --depth=0

@gregorskii
Copy link

gregorskii commented Oct 9, 2023

Might be helpful for some that used .nvmrc's and autoload ZSH hooks:

autoload -U add-zsh-hook
add-zsh-hook chpwd load-nvmrc
load-nvmrc() {
  if [[ -f .nvmrc && -r .nvmrc ]]; then
    version=$(cat .nvmrc)
    echo "Installing and using NodeJS $version from .nvmrc"
    if asdf install nodejs $version; then
      asdf local nodejs $version
    else
      echo "Issue installing NodeJS version $version"
    fi
  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment