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 |
a list of labels, which indicate the package.json files that will be installed
when you manually run the package manager, e.g. with
|
node_version |
optional; the specific version of NodeJS to install. |
yarn_version |
optional; the specific version of Yarn to install. |
vendored_node |
optional; the local path to a pre-installed NodeJS runtime. |
vendored_yarn |
optional; the local path to a pre-installed yarn tool. |
node_repositories |
optional; custom list of node repositories to use. |
yarn_repositories |
optional; custom list of yarn repositories to use. |
node_urls |
optional; custom list of URLs to use to download NodeJS. |
yarn_urls |
optional; custom list of URLs to use to download Yarn. |
preserve_symlinks |
Turn on --node_options=--preserve-symlinks for nodejs_binary and nodejs_test rules. The default for this is currently True but the options is deprecated and will be removed in the future. When this option is turned on, node will preserve the symlinked path for resolves instead of the default behavior of resolving to the real path. This means that all required files must be in be included in your runfiles as it prevents the default behavior of potentially resolving outside of the runfiles. For example, all required files need to be included in your node_modules filegroup. This option is desirable as it gives a stronger guarantee of hermiticity which is required for remote execution. |