rules_nodejs
rules_nodejs copied to clipboard
Import by package name isn't working in the latest nodejs rules.
🐞 bug report
Affected Rule
js_library
I believe.
Is this a regression?
Not sure.
Description
Here's my repro repo: https://github.com/bhavitsharma/bazel-bug-repro.
I have a shared package rule //repro-bz/b:lib
(exported as package_name = ts_project_lib
) defined in repro-bz/b/BUILD
. I am trying to import it inside a
package by its name, using the ts_project
and then running the binary using nodejs_binary
rule (//repro-bz/a:a_bin
). However, bazel always runs into this issue:
Error: Cannot find module 'ts_project_lib'
Require stack:
- /home/devbox/.cache/bazel/_bazel_devbox/896b6f083ac16d17d7dbf022a21d08e7/execroot/relicx/bazel-out/k8-fastbuild/bin/repro-bz/a/a_bin.sh.runfiles/relicx/repro-bz/a/lib.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (repro-bz/a/lib.js:3:26)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
🔬 Minimal Reproduction
https://github.com/bhavitsharma/bazel-bug-repro
🔥 Exception or Error
Two errors as mentioned in the description.
🌍 Your Environment
Operating System:
Ubuntu Linux
Output of bazel version
:
5.0.0
Rules_nodejs version:
5.1.0
Anything else relevant?
Just taking a quick guess here: it's probably because you don't have a "main"
property in your package.json
file in package 'b'. The node require()
algorithm looks for either the "main" entry in the package.json file, or otherwise looks for a file named index.js
(which you won't have since your file is named lib.ts
, which will be built into lib.js
)
The other possibility is that Bazel isn't creating the node_modules/b
folder in the build sandbox. Try using the --sandbox_debug
flag when building and inspect the sandbox folder after you hit the error and see if this exists or not.
Best, Greg
Aha, that was the issue. I created an index.js and it worked! Thank you! Is there a way to specify the main option you mentioned in the js_library rule without creating a package json?
It doesn't look like that's currently an option of js_library. I am thinking of proposing a new rule to auto-generate a package.json file though based on a package_name, "main"/"module" js files, and the dependencies that are imported into the package's source files. More to come on that.
In the meantime, I'd recommend either always having an index.js file in the root of the package or your package.json file having a "main" entry.
Best, Greg
This issue has been automatically marked as stale because it has not had any activity for 6 months. It will be closed if no further activity occurs in 30 days. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!
This issue was automatically closed because it went 30 days without any activity since it was labeled "Can Close?"