This article shows how to build a plain JavaScript demo with Bazel and Node.js. BTW I can recommend The Modern JavaScript Tutorial if you are new to JavaScript and want to learn it.
We will create a file name hello_world.js and add the following code:
console.log("Hello World!");
console.log(`Current Node.js version: ${process.version}`);
Along with that, we will create some Bazel-related build files. First of all, we will create a file with the filename.bazelversion with the content 8.3.0. This file is for Bazelisk. I assume you have Bazelisk installed on your system. Usually, one installs Bazelisk instead of Bazel since this makes version selection of Bazel, when switching between different projects easier. Also, it documents which Bazel version has been used to build the corresponding project. This can be helpful in a few years to figure out how high are the migration cost If you need to adapt to a newer or older version.
Next, we create a MODULE.bazel file with the following content:
bazel_dep(name = "aspect_rules_js", version = "2.3.8")
bazel_dep(name = "rules_nodejs", version = "6.4.0")
# Override with specific commit
git_override(
module_name = "rules_nodejs",
commit = "6b7609179ac1728c6e102d0050117a9b71e55fa5",
remote = "https://github.com/bazel-contrib/rules_nodejs",
)
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node", dev_dependency = True)
node.toolchain(node_version = "22.17.0")
As you can see from the MODULE.bazel file we use aspect_rules_js to have language rules for JavaScript. Also we define a specific node version (22.17.0). Since this node version was not included in rules_nodejs version 6.4.0 we do a git_override to fetch a slightly newer version of rules_nodejs that has the Node.js version 22.17.0. Check the Node.js website to figure out what is currently the newest LTS version.
Once this is set up we can create a BUILD.bazel file:
load("@aspect_rules_js//js:defs.bzl", "js_binary")
js_binary(
name = "hello_world",
entry_point = "hello_world.js",
)
Now we have everything in place to run our JavaScript program using Node.js:
bazel run //:hello_world
This should give you:
Hello World!
Current Node.js version: v22.17.0
There is no need to preinstall Node.js. Bazel will fetch it for you. Even if you have a local installed Node.js version with a different version number Bazel will not care. The above program will be created in a sandbox and be independent of whatever is installed on your system.
Here is a shell script to recreate the above demo:
mkdir js_hello_world
cd js_hello_world
echo "8.3.0" > .bazelversion
cat << EOF > hello_world.js
console.log("Hello World!");
console.log(`Current Node.js version: ${process.version}`);
EOF
cat << EOF > MODULE.bazel
bazel_dep(name = "aspect_rules_js", version = "2.3.8")
bazel_dep(name = "rules_nodejs", version = "6.4.0")
git_override(
module_name = "rules_nodejs",
commit = "b32837c0c3b9777fdadba9491cfd48fee2a16f27",
remote = "https://github.com/bazel-contrib/rules_nodejs",
)
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node", dev_dependency = True)
node.toolchain(node_version = "22.17.0")
EOF
cat << EOF > BUILD.bazel
load("@aspect_rules_js//js:defs.bzl", "js_binary")
js_binary(
name = "hello_world",
entry_point = "hello_world.js",
)
EOF
bazel run //:hello_world
Besides Node.js there are other runtimes runtime environments for JavaScript. For instance, there is Deno. You can use rules_deno to use Deno instead of Node.js with Bazel.