rules_nodejs icon indicating copy to clipboard operation
rules_nodejs copied to clipboard

ESM modules not working with linker

Open duarten opened this issue 2 years ago • 3 comments

🐞 bug report

Affected Rule

nodejs_binary

Is this a regression?

No.

Description

When using ESM modules, importing a js_library with a defined package_name fails.

🔬 Minimal Reproduction

Consider the following files:

a.js:

export const a = "10"

b.js:

import { a } from "@foo/bar/a.js"
console.log(a)

package.json:

{
    "type": "module"
}

BUILD.bazel:

load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "nodejs_binary")

js_library(
    name = "a",
    package_name = "@foo/bar",
    srcs = [
        "a.js",
        "package.json",
    ],
)

js_library(
    name = "b",
    srcs = [
        "b.js",
        "package.json",
    ],
    deps = ["a"],
)

nodejs_binary(
    name = "repro",
    data = [
        "b",
    ],
    entry_point = "b.js",
)

Note that commenting out the package_name and attribute and importing a.js by relative path works perfectly.

🔥 Exception or Error

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@foo/bar'

🌍 Your Environment

Operating System:

  
  macOS
  

Output of bazel version:

  
 5.1.0
  

Rules_nodejs version:

(Please check that you have matching versions between WORKSPACE file and @bazel/* npm packages.)

  
  5.4.0
  

Anything else relevant?

duarten avatar Apr 06 '22 18:04 duarten

Seems like this is working in 5.4.1! :)

duarten avatar Apr 29 '22 00:04 duarten

Spoke too soon, on linux I still hit this.

duarten avatar Apr 29 '22 04:04 duarten

I can work around this by specifying --node_options=--preserve-symlinks-main. I wonder if this should be done by default?

duarten avatar Apr 29 '22 04:04 duarten

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!

github-actions[bot] avatar Oct 30 '22 03:10 github-actions[bot]

This issue was automatically closed because it went 30 days without any activity since it was labeled "Can Close?"

github-actions[bot] avatar Nov 30 '22 02:11 github-actions[bot]