Install NodeJS & Yarn
Bazel

Install NodeJS & Yarn


Overview

This is a set of repository rules for setting up hermetic copies of NodeJS and Yarn. See https://docs.bazel.build/versions/master/skylark/repository_rules.html


node_repositories

node_repositories(package_json, node_version, yarn_version, vendored_node, vendored_yarn, node_repositories, yarn_repositories, node_urls, yarn_urls, preserve_symlinks)

To be run in user's WORKSPACE to install rules_nodejs dependencies.

This rule sets up node, npm, and yarn.

The versions of these tools can be specified in one of three ways:

  • Normal Usage: Specify no explicit versions. This will download and use the latest NodeJS & Yarn that were available when the version of rules_nodejs you're using was released.
  • Forced version(s): You can select the version of NodeJS and/or Yarn to download & use by specifying it when you call node_repositories, but you must use a value that matches a known version.
  • Using a custom version: You can pass in a custom list of NodeJS and/or Yarn repositories and URLs for node_resositories to use.
  • Using a local version: To avoid downloads, you can check in vendored copies of NodeJS and/or Yarn and set vendored_node and or vendored_yarn to point to those before calling node_repositories.

This rule exposes the @nodejs workspace containing some rules the user can call later:

  • Run node: bazel run @nodejs//:node path/to/program.js
  • Install dependencies using npm: bazel run @nodejs//:npm install
  • Install dependencies using yarn: bazel run @nodejs//:yarn

This rule also exposes the @yarn workspace for backwards compatibility:

  • Alternately install dependencies using yarn: bazel run @yarn//:yarn

Note that the dependency installation scripts will run in each subpackage indicated by the package_json attribute.

This approach uses npm/yarn as the package manager. You could instead have Bazel act as the package manager, running the install behind the scenes. See the npm_install and yarn_install rules, and the discussion in the README.

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")
node_repositories(package_json = ["//:package.json", "//subpkg:package.json"])

Running bazel run @nodejs//:yarn in this repo would create /node_modules and /subpkg/node_modules.

Attributes

package_json

List of strings; Optional

a list of labels, which indicate the package.json files that will be installed when you manually run the package manager, e.g. with bazel run @nodejs//:yarn or bazel run @nodejs//:npm install. If you use bazel-managed dependencies, you can omit this attribute.

node_version

Unknown; Optional

optional; the specific version of NodeJS to install.

yarn_version

Unknown; Optional

optional; the specific version of Yarn to install.

vendored_node

Unknown; Optional

optional; the local path to a pre-installed NodeJS runtime.

vendored_yarn

Unknown; Optional

optional; the local path to a pre-installed yarn tool.

node_repositories

Unknown; Optional

optional; custom list of node repositories to use.

yarn_repositories

Unknown; Optional

optional; custom list of yarn repositories to use.

node_urls

Unknown; Optional

optional; custom list of URLs to use to download NodeJS.

yarn_urls

Unknown; Optional

optional; custom list of URLs to use to download Yarn.