如何在Linux上高效安装和使用NVM

NVM for Linux:简介与安装

NVM,全称为Node Version Manager,是用于管理Node.js版本的工具。它可以帮助开发者在单一系统上轻松切换多个Node.js版本,尤其在项目需要特定Node.js版本时非常实用。

安装NVM

  1. 更新系统:首先,确保你的Linux系统是最新的,运行以下命令:

bash

sudo apt-get update && sudo apt-get upgrade

  1. 添加存储库:由于NVM不是标准Linux包,需要手动安装。使用curlwget运行安装脚本:

bash

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

或者:

bash

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

  1. 安装过程:上述命令会创建.nvm目录,并将其添加到.bashrc.zshrc文件中。重新加载配置文件:

bash

source ~/.bashrc

  1. 验证安装:输入以下命令验证NVM是否安装成功:

bash

nvm --version

使用NVM

  1. 安装Node.js版本:使用NVM安装最新稳定版Node.js:

bash

nvm install node

或者安装指定版本,如v14.17.6:

bash

nvm install 14.17.6

  1. 切换Node.js版本:使用以下命令切换版本:

bash

nvm use 14.17.6

  1. 查看已安装版本:输入以下命令查看已安装的Node.js版本:

bash

nvm ls

  1. 设置默认版本:设置默认Node.js版本:

bash

nvm alias default 14.17.6

  1. 卸载Node.js:要卸载指定的Node.js版本,使用以下命令:

bash

nvm uninstall 14.17.6

NVM的高级用法

  • 全局安装:如果需要全局安装Node.js,使用以下命令:

bash

nvm install --lts --reinstall-packages-from=system

  • 离线安装:若网络不稳定,可以先下载Node.js二进制文件再通过NVM安装:

bash

nvm install --download-only

nvm install

  • 使用镜像源:在中国,切换到国内镜像源可以加快下载速度:

bash

export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node

  • 自定义安装路径:可以通过环境变量NVM_DIR指定NVM的安装路径。

NVM的强大功能让开发者可以高效管理Node.js版本,减少因版本不兼容而引发的开发问题。

folder
nvm for linux版本 预估大小:323个文件
file
.editorconfig 523B
file
.mailmap 51B
file
LICENSE.md 1KB
file
.travis.yml 3KB
file
SECURITY.md 157B
file
ISSUE_TEMPLATE.md 1KB
file
require-allow-edits.yml 434B
file
windows-npm.yml 4KB
file
release.yml 932B
file
shellcheck.yml 1KB
file
rebase.yml 458B
file
latest-npm.yml 2KB
file
toc.yml 955B
file
lint.yml 2KB
file
FUNDING.yml 574B
file
.gitattributes 9B
file
Makefile 5KB
file
nvm.sh 136KB
file
GOVERNANCE.md 467B
file
update_test_mocks.sh 2KB
file
CONTRIBUTING.md 1KB
file
package.json 2KB
file
bash_completion 2KB
file
Dockerfile 4KB
file
CODE_OF_CONDUCT.md 5KB
file
install.sh 15KB
file
rename_test.sh 1KB
file
teardown 112B
file
Sourcing nvm.sh should keep version if one is active 592B
file
Sourcing nvm.sh with no default should return 0 314B
file
Sourcing nvm.sh should use the default if available and no nvm node is loaded 1KB
file
Sourcing nvm.sh with --no-use should not use anything 992B
file
Sourcing nvm.sh global alias bug 863B
file
setup 373B
file
Sourcing nvm.sh with --install should install the default 1KB
file
Sourcing nvm.sh with --install and .nvmrc should install it 850B
file
common.sh 2KB
file
install latest npm 2KB
file
install from source 834B
file
install with --latest-npm 269B
file
install two versions and use the latest one 682B
file
install version specified in .nvmrc from source 514B
file
install while reinstalling packages 2KB
file
install from source with thread parameter 950B
file
default-packages 3KB
file
install from source without V8 snapshot for ARM 762B
file
install from source implicitly 587B
file
setup_dir 62B
file
install from binary 678B
file
install hook 1KB
file
install LTS 1KB
file
install with --alias 244B
file
install version specified in .nvmrc from binary 510B
file
install already installed uses it 705B
file
teardown_dir 268B
file
install with --default 251B
file
install from binary with binary flag set 1KB
file
teardown 196B
file
nvm_is_merged_node_version 426B
file
nvm_ls_remote LTS aliases 1KB
file
nvm version-remote 2KB
file
nvm_is_valid_version 625B
file
nvm_version_greater 387B
file
nvm_compute_checksum 811B
file
nvm_get_minor_version 1023B
file
nvm_get_arch 2KB
file
nvm_print_implicit_alias errors 1KB
file
nvm_get_colors 3KB
file
nvm_strip_path 759B
file
nvm_ensure_version_prefix 639B
file
nvm_alias_path 164B
file
nvm_install_latest_npm 1KB
file
nvm_print_color_code 641B
file
nvm_use_if_needed 496B
file
nvm_alias 1KB
file
nvm_compare_checksum 3KB
file
nvm_tree_contains_path 1021B
file
nvm_install_binary_extract 2KB
file
nvm ls-remote 2KB
file
nvm_command_info 2KB
file
nvm_ensure_version_installed 3KB
file
nvm_is_iojs_version 381B
file
nvm_remote_version 3KB
file
nvm_clang_version 2KB
file
nvm_ensure_default_set 523B
file
nvm_get_latest missing curl or wget 455B
file
nvm_is_alias 520B
file
nvm_version_dir 608B
file
nvm_node_prefix 178B
file
nvm_supports_xz 884B
file
nvm_print_alias_path 2KB
file
nvm_get_checksum 1KB
file
nvm_print_implicit_alias success 3KB
file
nvm_remote_versions 3KB
file
nvm_ls_remote nightly 3KB
file
nvm_alias LTS-N 692B
file
nvm_get_default_packages 2KB
file
nvm_print_npm_version 623B
file
nvm_has 352B
file
nvm_curl_libz_support 1KB
file
nvm_strip_iojs_prefix 521B
file
nvm_install_no_progress_bar 1KB
file
nvm_has_system_node 354B
file
nvm_validate_implicit_alias 1KB
file
nvm_node_version_has_solaris_binary 1KB
file
nvm_num_version_groups 1KB
file
nvm_has_system_iojs 375B
file
nvm_download 513B
file
nvm_print_default_alias 1KB
file
nvm_get_checksum_alg 272B
file
nvm_stdout_is_terminal 2KB
file
nvm_iojs_prefix 178B
file
nvm_version_path 692B
file
nvm_is_version_installed 1KB
file
nvm_version 2KB
file
nvm_die_on_prefix 10KB
file
nvm_curl_version 865B
file
nvm_change_path 2KB
file
nvm_ls_remote_iojs 1KB
file
nvm_normalize_lts 1KB
file
nvm_get_artifact_compression 661B
file
nvm_get_mirror 1KB
file
nvm_has_solaris_binary 963B
file
nvm_find_project_dir 1KB
file
nvm_get_checksum_binary 399B
file
nvm_has_non_aliased 840B
file
nvm_format_version 655B
file
nvm_add_iojs_prefix 408B
file
nvm_is_natural_num 591B
file
nvm_check_for_help 306B
file
iojs.org-dist-index.tab 7KB
file
nvm ls-remote node.txt 14KB
file
nvm_print_implicit_alias remote stable nightly.txt 5B
file
nvm_ls_remote stable nightly.txt 34B
file
nvm ls-remote lts.txt 7KB
file
nvm_ls_remote LTS nightly argon.txt 4B
file
nvm ls-remote iojs.txt 656B
file
lts-star.txt 12B
file
nvm_print_implicit_alias remote stable.txt 5B
file
nvm_ls_remote.txt 6KB
file
nvm_ls_remote LTS nightly.txt 1KB
file
nvm_make_alias LTS alias calls.txt 155B
file
nvm ls-remote.txt 15KB
file
LTS_names.txt 50B
folder
.gitkeep 文件夹
folder
package.json 文件夹
folder
.gitkeep 文件夹
folder
.gitkeep 文件夹
folder
.gitkeep 文件夹
folder
.gitkeep 文件夹
folder
package.json 文件夹
folder
package.json 文件夹
folder
.gitkeep 文件夹
folder
package.json 文件夹
folder
.gitkeep 文件夹
folder
.gitkeep 文件夹
folder
package.json 文件夹
folder
.gitkeep 文件夹
file
nvm_ls_remote nightly.txt 5KB
file
nodejs.org-dist-index.tab 133KB
file
nvm_ls_remote stable.txt 8B
file
nvm_ls_remote LTS argon.txt 470B
file
nvm_ls_remote_iojs.txt 492B
file
nodejs.org-download-nightly-index.tab 761KB
file
nvm_ls_remote LTS.txt 2KB
file
nvm_find_up 817B
file
nvm_install_binary_nosource 953B
file
nvm_make_alias 770B
file
nvm_get_download_slug 4KB
file
nvm_ls_current 2KB
file
nvm set_colors 2KB
file
nvm_ls_remote 3KB
file
nvm_iojs_version_has_solaris_binary 596B
file
nvm_curl_use_compression 2KB
file
Running 'nvm alias' should create a file in the alias directory 109B
file
Sourcing nvm.sh should not modify parameters of caller 62B
file
teardown 412B
file
'nvm alias' should not accept aliases with slashes 1KB
file
'nvm unalias' should not accept aliases with names equal to built-in alias 1KB
file
Running 'nvm alias' lists implicit aliases when they do not exist 1KB
file
Running 'nvm alias ˂aliasname˃ ˂target˃' again should change the target 864B
file
Running 'nvm alias' lists manual aliases instead of implicit aliases when present 2KB
file
Running 'nvm alias' should list all aliases 3KB
file
nvm_resolve_alias 1KB
file
Running 'nvm alias ˂aliasname˃' should list but one alias 75B
file
nvm_list_aliases works with LTS aliases 585B
file
nvm_ensure_default_set 695B
file
setup_dir 191B
file
'nvm alias' should ensure LTS alias dir exists 355B
file
teardown_dir 195B
file
'nvm unalias' should not accept aliases with slashes 1KB
file
nvm_resolve_local_alias 2KB
file
nvm_list_aliases calls nvm_get_colors 514B
file
nvm_print_formatted_alias calls nvm_get_colors 648B
file
teardown 161B
file
nvm_resolve_alias 2KB
file
nvm_resolve_local_alias 2KB
file
setup 193B
file
nvm_list_aliases works with no LTS aliases present 375B
file
'nvm unalias' should accept aliases when they shadow a built-in alias 658B
file
setup 294B
file
nvm_print_alias_path calls nvm_get_colors 647B
file
Running 'nvm use system' should work as expected 2KB
file
nvm should remove the last trailing slash in $NVM_DIR 395B
file
teardown 71B
file
Running 'nvm which foo' should return a nonzero exit code when not found 78B
file
Running 'nvm which 0.0.2' should display only version 0.0.2 854B
file
teardown 384B
file
Running 'nvm ls' should display all installed versions 1KB
file
Running 'nvm ls system' should include 'system' when appropriate 558B
file
Running 'nvm ls --no-alias' with a pattern errors 500B
file
Running 'nvm ls' with nounset should not fail 622B
file
Running 'nvm ls' should filter out '.nvm' 192B
file
Running 'nvm ls 0.2' should display only 0.2.x versions 861B
file
Running 'nvm ls' should list versions in the 'versions' directory 320B
file
Running 'nvm ls io' should return NA 56B
file
Running 'nvm ls' calls into nvm_alias 890B
file
Running 'nvm ls node_' should return a nonzero exit code when not found 59B
file
Running 'nvm ls' should include 'system' when appropriate 530B
file
Running 'nvm ls foo' should return a nonzero exit code when not found 73B
file
Running 'nvm ls' should filter out 'versions' 199B
file
Running 'nvm ls stable' and 'nvm ls unstable' should return the appropriate implicit alias 902B
file
Running 'nvm ls' should not show a trailing slash 779B
file
Running 'nvm ls --no-alias' does not call into nvm_alias 896B
file
Running 'nvm ls 0.0.2' should display only version 0.0.2 411B
file
Using a nonstandard IFS should not break 297B
file
Running 'nvm ls' with node-like versioning vx.x.x should only list a matched version 188B
file
Running 'nvm deactivate' should unset the nvm environment variables 973B
file
Running 'nvm unload' should unset all function and variables 685B
file
setup_dir 195B
file
Running 'nvm unalias' should remove the alias file 147B
file
Running 'nvm use iojs' uses latest io.js version 1KB
file
Running 'nvm use' should drop CR char automatically 512B
file
Running 'nvm use foo' where 'foo' is circular aborts 992B
file
Running 'nvm use x' should not create the 'current' symlink if $NVM_SYMLINK_CURRENT is false 2KB
file
teardown_dir 157B
file
setup 189B
file
Running 'nvm current' should display current nvm environment 229B
file
Sourcing nvm.sh should make the nvm command available 41B
file
nvm_print_default_alias calls nvm_get_colors 581B
file
nvm_print_versions calls nvm_get_colors 1KB
file
nvm_err_with_colors 331B
file
nvm_echo_with_colors 342B
file
Running 'nvm uninstall' with incorrect file permissions fails nicely 320B
file
Running 'nvm use x' should create and change the 'current' symlink 770B
file
Running 'nvm install' with '--reinstall-packages-from' requires a valid version 2KB
file
Running 'nvm install' with an invalid version fails nicely 375B
file
Running 'nvm uninstall' should remove the appropriate directory 116B
file
install from source 664B
file
install two versions and use the latest one 924B
file
install version specified in .nvmrc from source 632B
file
install while reinstalling packages 1KB
file
setup_dir 62B
file
install from binary 996B
file
install version specified in .nvmrc from binary 652B
file
install already installed uses it 1007B
file
teardown_dir 243B
file
nvm install v1 works 679B
file
nvm_check_global_modules 2KB
file
nvm_reset 1KB
file
nvm_do_install 187B
file
nvm_install_with_aliased_dot 454B
file
install_nvm_from_git 4KB
file
nvm_profile_is_bash_or_zsh 724B
file
nvm_source 3KB
file
nvm_install_with_node_version 303B
file
nvm_download 531B
file
nvm_install_dir 677B
file
nvm_detect_profile 4KB
file
setup_dir 49B
file
Running 'nvm uninstall 0.12.6' uninstalls v0.12.6 739B
file
teardown_dir 66B
file
Running 'nvm uninstall' with incorrect file permissions fails nicely 849B
file
Running 'nvm copy-packages $(nvm ls current)' should error out 767B
file
works with no installs 460B
file
should work as expected 1KB
file
setup_dir 158B
file
teardown_dir 188B
file
package.json 191B
file
index.js 38B
file
nvm_get_latest 5KB
file
nvm_get_latest failed redirect 517B
file
Running 'nvm run 0.x' should error out sensibly when 0.x is not installed 618B
file
setup_dir 144B
file
Running 'nvm run --lts' should work 579B
file
Running 'nvm run --harmony --version' should work 258B
file
Running 'nvm run' should pick up .nvmrc version 353B
file
Running 'nvm run 0.x' should work 180B
file
teardown_dir 181B
file
Running 'nvm exec --lts' should work 549B
file
Running 'nvm exec' should pick up .nvmrc version 506B
file
setup_dir 137B
file
Running 'nvm exec 0.x' should work 500B
file
Preamble works and respects 'silent' flag 1KB
file
teardown_dir 175B
file
Running 'nvm exec' with help should not parse 206B
file
Running 'nvm use --lts' uses latest LTS version 567B
file
Running 'nvm use node' uses latest stable node version 546B
file
Running 'nvm use v1.0.0' uses iojs-v1.0.0 iojs version 560B
file
setup_dir 530B
file
Running 'nvm use iojs' uses latest io.js version 536B
file
Running 'nvm use --lts=foo' uses latest 'foo' LTS version 613B
file
teardown_dir 519B
file
Running 'nvm use node --silent' doesn't print anything 338B
file
Running 'nvm use' calls 'nvm_die_on_prefix' 642B
file
Running 'nvm current' should display current nvm environment 203B
file
node 0.6.21 should install 0.6.21-pre 324B
file
pkg_info_x86 12B
file
pkg_info_amd64 153B
file
pkg_info_fail 7B
file
uname_smartos_amd64 123B
file
uname_smartos_x86 123B
file
isainfo_amd64 70B
file
isainfo_x86 63B
file
uname_osx_amd64 188B
file
uname_osx_x86 182B
file
PROJECT_CHARTER.md 3KB
file
nvm-exec 351B
file
ROADMAP.md 882B
file
.gitignore 253B
file
.dockerignore 140B
file
.npmrc 19B
file
README.md 40KB
...
gz 文件大小:162.93KB