简体   繁体   中英

Bazel Typescript - Cannot find ts_library Referenced by module_name

I have two Bazel BUILD files:

  • server/BUILD
  • enums/BUILD

My goal is to import a ts_library from enums into server as a dependency for the server ts_library . Therefore, I used the approach described here: https://dev.to/lewish/building-a-typescript-monorepo-with-bazel-4o7n (via module_name and deps )

Enums BUILD file:

package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "enums",
    srcs = glob(["src/index.ts"]),
    module_name = "@lbm/enums",
)

Server BUILD file:

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "lib",
    srcs = glob(
        include = [ "**/*.ts" ],
        exclude = [ "src/index.ts" ],
    ),
    deps = ["//enums:enums"]
)

ts_library(
    name = "main",
    srcs = [ "src/index.ts" ],
    deps = ["//enums:enums", ":lib"],
)

filegroup(
    name = "libfiles",
    srcs = ["lib"],
    output_group = "es5_sources",
)

filegroup(
    name = "mainfile",
    srcs = ["main"],
    output_group = "es5_sources",
)

nodejs_image(
    name = "server",
    data = [
        ":libfiles",
        ":mainfile",
    ],
    entry_point = ":mainfile",
)

But when running

bazel run //server:server

Although I've set module_name = "@lbm/enums" and added //enums to the deps , I get this error:

INFO: Analyzed target //server:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /minimal-bazel-monorepo/server/BUILD:13:1: Compiling TypeScript (devmode) //server:main failed (Exit 1)
server/src/index.ts:2:27 - error TS2307: Cannot find module '@lbm/enums'.

2 import { Constants } from '@lbm/enums';
                            ~~~~~~~~~~~~

Target //server:server failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.207s, Critical Path: 0.09s
INFO: 0 processes.

You can try it yourself: https://github.com/flolude/minimal-bazel-monorepo

Edit #1

After updating the import statement from import { Constants } from '@lbm/enums'; to import { Constants } from '@lbm/enums/src'; , as stated by Ray , I get this error:

Error: Cannot find module '@lbm/enums/src/index'. Please verify that the package.json has a valid "main" entry
  1. Since BUILD file is located in /enums dir and not in /enums/src, the correct TS import would look like this: import { Constants } from '@lbm/enums/src';
    Or maybe consider moving this BUILD file to /enums/src dir, then TS imports can be left as of now.
  2. I noticed from the example that you use yarn workspaces. Unfortunately, they are not supported right now by rules_nodejs, so prob better to have one package.json in the root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM