rules_nodejs
rules_nodejs copied to clipboard
node_patches.js causes Error [ERR_REQUIRE_ESM] when package.json is type="module"
π bug report
Affected Rule
The issue is caused by the rule:nodejs_binary - specifically, the underlying auto generated rule from load("@npm//<pkg>:index.bzl", ...)
Is this a regression?
Yes, the previous version in which this bug was not present was: ....Nope, suspect this has always been the case.
Description
A clear and concise description of the problem...I've been working with setups that require my package.json to declare type="module". When I do this, bazel build fails with:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /private/var/tmp/_bazel_martaver/b0760405c537286aa4a81be9bf51a1b4/sandbox/darwin-sandbox/1/execroot/cleric/bazel-out/host/bin/external/npm/@sveltejs/kit/bin/svelte-kit.sh.runfiles/build_bazel_rules_nodejs/internal/node/node_patches.js
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at Module._preloadModules (internal/modules/cjs/loader.js:1231:12)
at loadPreloadModules (internal/bootstrap/pre_execution.js:468:5)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:71:3)
at internal/main/run_main_module.js:7:1 {
code: 'ERR_REQUIRE_ESM'
}
Target //apps/sveltekit-www:build failed to build
Discussion with @matthewjh on slack revealed:
not really a bug, but the way it is at the moment. package.json[type=module] mandates that all .js files underneath the package.json directory be ESM. unfortunately, one of the files rules_nodejs generates (node_patches.js) is CJS. i'm not sure whether the ultimate fix may be as simple as renaming it to node_patches.cjs. probably not as I recall node_patches.js is a require callsite, meaning it will load other modules, which will. not work if they are ESM (edited)
π¬ Minimal Reproduction
https://github.com/cleric-sh/repro/tree/rules_nodejs-esm
π₯ Exception or Error
ERROR: /Users/martaver/Projects/org/cleric/git/repro/BUILD.bazel:3:6: Action foo failed: (Exit 1): babel.sh failed: error executing command bazel-out/host/bin/external/npm/babel-cli/bin/babel.sh '--bazel_node_modules_manifest=bazel-out/darwin-fastbuild/bin/_foo.module_mappings.json'
Use --sandbox_debug to see verbose messages from the sandbox
[link_node_modules.js] manifest file: bazel-out/darwin-fastbuild/bin/_foo.module_mappings.json
[link_node_modules.js] manifest contents: {
"workspace": "cleric",
"bin": "bazel-out/darwin-fastbuild/bin",
"roots": {
"": "npm"
},
"module_sets": {}
}
[link_node_modules.js] startCwd: /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric
[link_node_modules.js] execroot: /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric
[link_node_modules.js] isExecroot: true
[link_node_modules.js] resolved npm external workspace node modules path to /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/external/npm/node_modules
[link_node_modules.js] creating symlink node_modules -> /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/external/npm/node_modules
[link_node_modules.js] creating symlink /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/bazel-out/host/bin/external/npm/babel-cli/bin/babel.sh.runfiles/cleric/node_modules -> /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/external/npm/node_modules
BAZEL_NODE_MODULES_ROOTS=:npm
BAZEL_PATCH_ROOTS=/private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric,/private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/node_modules,/private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/bazel-out/host/bin/external/npm/babel-cli/bin/babel.sh.runfiles,/private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/bazel-out/host/bin/external/npm/babel-cli/bin/babel.sh.runfiles/cleric/node_modules,/private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/external/npm/node_modules
internal/modules/cjs/loader.js:1089
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /private/var/tmp/_bazel_martaver/055eea68534e08b81328223ececeada3/sandbox/darwin-sandbox/4/execroot/cleric/bazel-out/host/bin/external/npm/babel-cli/bin/babel.sh.runfiles/build_bazel_rules_nodejs/internal/node/node_patches.js
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at Module._preloadModules (internal/modules/cjs/loader.js:1231:12)
at loadPreloadModules (internal/bootstrap/pre_execution.js:468:5)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:71:3)
at internal/main/run_main_module.js:7:1 {
code: 'ERR_REQUIRE_ESM'
}
Target //:foo failed to build
Use --verbose_failures to see the command lines of failed build steps.
π Your Environment
Operating System:
MacOS 11.2.3
Output of bazel version:
bazel 4.2.2
Rules_nodejs version:
(Please check that you have matching versions between WORKSPACE file and @bazel/* npm packages.)
4.4.6
Anything else relevant?
I've made a issue here #3277 to discuss ESM support in rules_nodejs - with support added it would fix this issue
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?"