Skip to content

关于 Volta!

介绍

欢迎来到 Volta!

Volta 是一种管理 JavaScript 命令行工具的便捷方式。

功能

  • 速度
  • 无缝,每个项目的版本切换
  • 跨平台支持,包括 Windows 和所有 Unix shell
  • 支持多个包管理器
  • 稳定的工具安装-无需每次升级都重新安装!
  • 可扩展性挂钩用于特定于站点的定制

为什么选择 Volta?

Volta’s job is to get out of your way.

使用 Volta,您可以一次选择 Node 引擎,然后不再担心它。您可以在项目之间切换,而不必手动在节点之间切换。你可以在工具链中安装 npm 二进制包,而不必定期重新安装它们,或者弄清楚它们停止工作的原因。

快速设置和切换 Node 引擎

获取并使用特定版本的 Node:

bash
volta install node@14.18.1

您应该立即注意到该工具的响应速度有多快。您的开发时间是宝贵的!JavaScript 开发人员应该拥有时髦的工具。 🙂

为合作者提供可复制的环境

Volta 允许你用一个命令为一个项目选择节点引擎和包管理器:

bash
volta pin node@14.18.1

Volta 将 Node 引擎的准确版本保存在 package.json,这样你就可以把你的选择提交给 git。从那时起,每次在项目目录中运行 Node 时,Volta 都会自动切换到您选择的同一版本 的 Node。类似的。所有的合作者都可以通过在他们的开发机器上安装 Volta 来做同样的事情。

Install and forget

Volta 还允许您将最喜欢的二进制包作为命令行工具安装,而不必担心它们会干扰您的开发项目。更好的是,这些工具在安装时被固定到特定的 Node 引擎上,除非您显式地告诉它们,否则它们不会更改。这意味着一旦一个工具工作了,它就会继续工作。

bash
npm install -g surge
surge -h

如何工作的

Volta 没有使用任何花哨的操作系统特性或特定于 shell 的钩子。

它是建立在简单,行之有效的方法垫片。每当您使用 Volta 安装工具时,它都会为您的 PATH 添加一个垫片,该垫片充当到正确版本的工具的智能(快速)路由器,并使用正确的 Node 引擎运行它。

Volta 很容易安装,没有外部依赖,因为它是在 Rust 中作为一个单一的、快速的本地可执行文件构建的。

快速开始

安装 Volta

linux 安装

在大多数 Unix 系统(包括 macOS)上,您可以使用一个命令安装 Volta:

bash
curl https://get.volta.sh | bash
  • 配置环境变量:
bash
export PATH="$HOME/.volta/bin:$PATH"

Windows 安装

对于 Windows,下载并运行 Windows 安装程序并按照说明操作。

TIP

提示

Volta 的功能依赖于创建符号链接,所以你必须:

Windows 下的 linux 子系统

如果您在 Linux 的 Windows 子系统中使用 Volta,请遵循上面的 Unix 安装指南。

选择默认的 Node 版本

这是 Volta 将在有固定版本的项目之外的其它地方使用的版本。

要选择特定版本的 Node,可以运行:

bash
volta install node@20.12.2

或者使用最新的 LTS 版本,可以运行:

bash
volta install node

Volta 命令

Volta Fetch

volta fetch 命令将允许您将工具获取到本地缓存中,而无需将其设置为默认值或使其可用,以供将来脱机使用。

它的语法如下:

bash
Fetches a tool to the local machine
USAGE:
    volta fetch [FLAGS] <tool[@version]>...
FLAGS:
        --verbose    Enables verbose diagnostics
        --quiet      Prevents unnecessary output
    -h, --help       Prints help information
ARGS:
    <tool[@version]>...    Tools to fetch, like `node`, `yarn@latest` or `your-package@^14.4.3`.

Volta install

volta install 命令将设置工具的默认版本。如果该工具尚未在本地缓存,它也将获取该工具。

它的语法如下:

bash
Installs a tool in your toolchain
USAGE:
    volta install [FLAGS] <tool[@version]>...
FLAGS:
        --verbose    Enables verbose diagnostics
        --quiet      Prevents unnecessary output
    -h, --help       Prints help information
ARGS:
    <tool[@version]>...    Tools to install, like `node`, `yarn@latest` or `your-package@^14.4.3`.

Volta uninstall

volta uninstall 命令允许您删除已安装在 volta install 中的任何全局包。

TIP

提示

从 Volta 0.9.0 开始,您还可以使用软件包管理器卸载全局软件包,如下 npm uninstall --global yarn global remove

sh
Uninstalls a tool from your toolchain
USAGE:
    volta uninstall [FLAGS] <tool>
FLAGS:
        --verbose    Enables verbose diagnostics
        --quiet      Prevents unnecessary output
    -h, --help       Prints help information
ARGS:
    <tool>    The tool to uninstall, e.g. `node`, `npm`, `yarn`, or <package>

Volta pin

volta pin命令将更新一个项目的 package.json 文件以使用工具的选定版本。

TIP

提示

volta pin只适用于 Node 和包管理器(例如 npm 或 Yarn)。对于依赖项,你应该使用 npm installyarn add 来更新选择的版本。

命令格式如下:

bash
Pins your project's runtime or package manager
USAGE:
    volta pin [FLAGS] <tool[@version]>...
FLAGS:
        --verbose    Enables verbose diagnostics
        --quiet      Prevents unnecessary output
    -h, --help       Prints help information
ARGS:
    <tool[@version]>...    Tools to pin, like `node@lts` or `yarn@^1.14`.

Volta list

命令格式如下:

bash
Displays the current toolchain
USAGE:
    volta list [FLAGS] [OPTIONS] [tool]
FLAGS:
    -c, --current
            Show the currently-active tool(s).
            Equivalent to `volta list` when not specifying a specific tool.
    -d, --default
            Show your default tool(s).
        --verbose
            Enables verbose diagnostics
        --quiet
            Prevents unnecessary output
    -h, --help
            Prints help information
OPTIONS:
        --format <format>
            Specify the output format.

            Defaults to `human` for TTYs, `plain` otherwise. [possible values: human, plain]
ARGS:
    <tool>
            The tool to lookup: `all`, `node`, `yarn`, or the name of a package or binary.

Volta completions

volta completions 命令将为您的 shell 生成命令完成信息。

它的语法如下

bash
Generates Volta completions
By default, completions will be generated for the value of your current shell,
shell, i.e. the value of `SHELL`. If you set the `<shell>` option, completions
will be generated for that shell instead.
If you specify a directory, the completions will be written to a file there;
otherwise, they will be written to `stdout`.
USAGE:
    volta completions [FLAGS] [OPTIONS] <shell>
FLAGS:
    -f, --force
            Write over an existing file, if any.
        --verbose
            Enables verbose diagnostics
        --quiet
            Prevents unnecessary output
    -h, --help
            Prints help information
OPTIONS:
    -o, --output <out_file>
            File to write generated completions to
ARGS:
    <shell>
            Shell to generate completions for [possible values: zsh, bash, fish, powershell,
            elvish]

Volta which

volta which 命令将打开 volta 的垫片并定位将由 volta 启动的实际二进制文件。

它的语法如下:

bash
Locates the actual binary that will be called by Volta
USAGE:
    volta which [FLAGS] <binary>
FLAGS:
        --verbose    Enables verbose diagnostics
        --quiet      Prevents unnecessary output
    -h, --help       Prints help information
ARGS:
    <binary>    The binary to find, e.g. `node` or `npm`

Volta setups

volta setup 命令将通过修改当前用户的 PATH(以与平台相关的方式)来启用 volta,使其包含 volta shim 目录。

Unix

在 Unix 上,volta 安装程序将使用以下列表搜索配置文件脚本:

  • ~/.profile
  • ~/.bash_profile
  • ~/.bashrc
  • ~/.zshrc
  • ~/.config/fish/config.fish
  • PROFILE 环境变量的值

对于存在的每个文件,volta 安装程序将对其进行修改,使其包含定义 VOLTA_HOME 的行,并将$VOLTA_HOME/bin 添加到 PATH 环境变量中。如果当前 shell 的配置文件 (使用 $SHELL 环境变量检测)不存在,那么将使用适当的内容创建它,如果可能的话。

Windows

在 Windows 上,volta 设置将修改 User Path 环境变量以包含 shim 目录(%LOCALAPPDATA%\Volta\bin).

语法

命令格式如下:

bash
Enables Volta for the current user
USAGE:
    volta setup [FLAGS]
FLAGS:
        --verbose    Enables verbose diagnostics
        --quiet      Prevents unnecessary output
    -h, --help       Prints help information

Volta run

volta run 命令将使用命令行中指定的工具版本运行您提供的命令。任何没有直接指定版本的工具都将由 Volta 通常的上下文检测来确定其版本,使用项目中的固定版本或默认版本。

--no-yarn 标志允许您覆盖该决定,并在命令运行时强制环境中不包含 yarn。类似地,--bundled-npm 标志允许你强制使用 npm 与 Node 绑定的版本,而忽略任何自定义版本。

TIP

提示

版本设置必须出现在要运行的命令之前。命令之后的任何内容都将被视为参数不会被 volta 读到

此外,您可以使用 --env NAME-value option 选项指定要传递给命令的自定义环境变量。

命令格式如下:

bash
Run a command with custom Node, npm, and/or Yarn versions
USAGE:
    volta run [FLAGS] [OPTIONS] <command> [--] [args]...
FLAGS:
        --bundled-npm    Forces npm to be the version bundled with Node
        --no-yarn        Disables Yarn
        --verbose        Enables verbose diagnostics
        --quiet          Prevents unnecessary output
    -h, --help           Prints help information
OPTIONS:
        --node <version>         Set the custom Node version
        --npm <version>          Set the custom npm version
        --yarn <version>         Set the custom Yarn version
        --env <NAME=value>...    Set an environment variable (can be used multiple times)
ARGS:
    <command>    The command to run
    <args>...    Arguments to pass to the command

Volta help

volta help 命令的语法如下:

bash
Prints this message or the help of the given subcommand(s)
USAGE:
    volta help [subcommand]...
ARGS:
    <subcommand>...    The subcommand whose help message to display

pnpm 支持

目前还没有自动迁移功能,因此如果您以前将 pnpm 作为 Volta 全局文件安装,则需要通过调用 Volta install pnpm 手动重新安装它。在启用支持并重新安装之前,您可以通过 volta uninstall pnpm 卸载之前安装的 pnpm 包。一旦切换到本机 pnpm 支持,由于目前缺乏卸载实现,可能无法通过调用相同的命令来删除孤立的旧 pnpm 包。

卸载 pnpm

bash
volta uninstall pnpm

安装 pnpm

bash
volta install pnpm

上次更新于: