溫馨提示×

Linux 中 Node.js 如何進行版本控制

小樊
43
2025-10-05 22:15:34
欄目: 編程語言

Using Node Version Manager (nvm)
nvm is the most widely used tool for managing multiple Node.js versions on Linux. It allows you to install, switch, and uninstall versions seamlessly.

  • Installation: Run curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash (or use wget), then reload your shell configuration with source ~/.bashrc (or ~/.zshrc). Verify installation with nvm --version.
  • Key Commands:
    • Install a specific version: nvm install 18.16.0 (replace with desired version).
    • List installed versions: nvm ls.
    • Switch to a version: nvm use 18.16.0.
    • Set a default version: nvm alias default 18.16.0.
    • Uninstall a version: nvm uninstall 18.16.0.
  • Project-Specific Version Control: Create a .nvmrc file in your project root with the desired version (e.g., 18.16.0). Running nvm use in the directory will automatically switch to the version specified in .nvmrc.

Using n (Node Version Switcher)
n is a lightweight tool for switching between Node.js versions, ideal for users who prefer simplicity. It requires Node.js to be pre-installed.

  • Installation: Run sudo npm install -g n to install globally via npm.
  • Key Commands:
    • Install the latest version: sudo n latest.
    • Install the latest LTS version: sudo n lts.
    • Install a specific version: sudo n 18.16.0.
    • Switch between installed versions: Run n, then select the version from the interactive list.
  • Note: n manages versions in /usr/local/n/versions/node, and switching versions updates the system-wide Node.js binary.

Using asdf
asdf is a generic version manager supporting multiple languages (including Node.js). It’s highly extensible and integrates well with existing shell environments.

  • Installation: Clone the asdf repository and add it to your shell configuration:
    git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
    echo -e "\n. $HOME/.asdf/asdf.sh" >> ~/.bashrc  # For bash
    echo -e "\n. $HOME/.asdf/completions/asdf.bash" >> ~/.bashrc
    source ~/.bashrc
    
  • Node.js Plugin: Install the Node.js plugin with asdf plugin-add nodejs.
  • Key Commands:
    • Install a version: asdf install nodejs 18.16.0.
    • Switch globally: asdf global nodejs 18.16.0 (sets the default for the system).
    • Switch locally (project-specific): asdf local nodejs 18.16.0 (creates a .tool-versions file in the project).
    • List installed versions: asdf list nodejs.

Using Volta
Volta is a modern tool designed for speed and simplicity, with built-in support for Node.js and npm version management. It automatically pins versions per project.

  • Installation: Run curl https://get.volta.sh | bash and restart your terminal.
  • Key Commands:
    • Install a version: volta install node@18.16.0.
    • Pin a version to the current project: volta pin node@18.16.0 (creates a package.json entry or .volta file).
    • Switch globally: volta pin node@18.16.0 (updates the user’s default).
    • List installed versions: volta list node.
  • Advantages: Volta ensures consistent versions across different shells and projects without requiring manual environment variable management.

Using fnm (Fast Node Manager)
fnm is a Rust-based tool optimized for performance, offering fast installation and switching of Node.js versions. It’s ideal for developers who prioritize speed.

  • Installation: Use the universal installer: curl -fsSL https://fnm.vercel.app/install | bash. Reload your shell with source ~/.bashrc (or ~/.zshrc).
  • Key Commands:
    • Install a version: fnm install 18.16.0.
    • Switch to a version: fnm use 18.16.0.
    • Set a default version: fnm default 18.16.0.
    • List installed versions: fnm list.
  • Performance: fnm uses a local cache and avoids spawning subprocesses, making it faster than many other tools.

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女