admin管理员组

文章数量:1415684

How do i use the nodejs_binary rule to do a standard npm run start. I am able to run a typical node project using this rule. However i want to run a the start script in package.json. So far i have the following below in my build file

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

nodejs_binary(
    name = "app",
    data = [":app_files"],
    node="@nodejs//:bin/npm",
    entry_point = "workspace_name/src/server.js",
    node_modules = "@npm_deps//:node_modules",
    args=["start"]
)

This does not start the server..somehow npm mand is not running properly. it indicates usage of the mand in inplete.

I am currently able to do this within the WORKSPACE

bazel run @nodejs//:bin/yarn (runs yarn install and installs all node-modulse)

bazel run @nodejs//:bin/npm start (this starts the server)

In my package.json i have

{
  "scripts": {
    "start": "babel-node src/server.js",
   ...
  }
...
}

I do i get this to work with nodejs_binary rule and subsequently node_image

I changed from using npm to using yarn..workspace_name/src/server.js.. is called now but Then i had different set of problems, babel-node was not found. I modified the rule a bit. After careful study...I realise that there is a dependency on babel-node that is not satisfied at the time yarn run start is called. The following worked after i had run bazel run @nodejs//:bin/yarn before running the rule.

nodejs_binary(
    name = "app",
    args = ["start"],
    data = [
        ":app_files",
        "@//:node_modules",
    ],
    entry_point = "workspace_name/src/server.js",
    node = "@nodejs//:bin/yarn",
    node_modules = "@npm_deps//:node_modules",
)

It appears that "@//:node_modules" solves the babel-node dependency issue. So the rule above does not work on its own...it needs me to do bazel run @nodejs//:bin/yarn (more like npm/yarn install to make the node_modules, which contain babel-node dependecy available when npm/yarn start is run)

So my problem is that I do not want to have to manually run bazel run @nodejs//:bin/yarn before executing my rule. how do i do this. I suppose it would work if i stopped depending on babel-node...but then i would have to change my code to not use es6 syntax (that is a hustle). Is there a way i can do this with a genrule? or something...

How do i use the nodejs_binary rule to do a standard npm run start. I am able to run a typical node project using this rule. However i want to run a the start script in package.json. So far i have the following below in my build file

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

nodejs_binary(
    name = "app",
    data = [":app_files"],
    node="@nodejs//:bin/npm",
    entry_point = "workspace_name/src/server.js",
    node_modules = "@npm_deps//:node_modules",
    args=["start"]
)

This does not start the server..somehow npm mand is not running properly. it indicates usage of the mand in inplete.

I am currently able to do this within the WORKSPACE

bazel run @nodejs//:bin/yarn (runs yarn install and installs all node-modulse)

bazel run @nodejs//:bin/npm start (this starts the server)

In my package.json i have

{
  "scripts": {
    "start": "babel-node src/server.js",
   ...
  }
...
}

I do i get this to work with nodejs_binary rule and subsequently node_image

I changed from using npm to using yarn..workspace_name/src/server.js.. is called now but Then i had different set of problems, babel-node was not found. I modified the rule a bit. After careful study...I realise that there is a dependency on babel-node that is not satisfied at the time yarn run start is called. The following worked after i had run bazel run @nodejs//:bin/yarn before running the rule.

nodejs_binary(
    name = "app",
    args = ["start"],
    data = [
        ":app_files",
        "@//:node_modules",
    ],
    entry_point = "workspace_name/src/server.js",
    node = "@nodejs//:bin/yarn",
    node_modules = "@npm_deps//:node_modules",
)

It appears that "@//:node_modules" solves the babel-node dependency issue. So the rule above does not work on its own...it needs me to do bazel run @nodejs//:bin/yarn (more like npm/yarn install to make the node_modules, which contain babel-node dependecy available when npm/yarn start is run)

So my problem is that I do not want to have to manually run bazel run @nodejs//:bin/yarn before executing my rule. how do i do this. I suppose it would work if i stopped depending on babel-node...but then i would have to change my code to not use es6 syntax (that is a hustle). Is there a way i can do this with a genrule? or something...

Share Improve this question edited Sep 18, 2018 at 12:41 Baama asked Sep 18, 2018 at 7:58 BaamaBaama 2,6427 gold badges46 silver badges73 bronze badges 2
  • Hi, I know Bazel but I don't know Node. Can you successfully build the rule and just not run it correctly? How exactly does the npm mand fail, is there an error message, or does it just run with the wrong arguments, or does Bazel run something else than you expect it? – László Commented Sep 18, 2018 at 8:51
  • @László You can check my updated question. But to answer your question..i can currently build and run but only under certain conditions. I hope my updates to the question makes better sense. – Baama Commented Sep 18, 2018 at 12:43
Add a ment  | 

1 Answer 1

Reset to default 4

What I ended up doing was that i made a babel nodejs_binary rule. Then used that to pile my source files in a gen rule

# Make babel binary
nodejs_binary(
    name = "babel",
    entry_point = "npm_deps/node_modules/babel-cli/bin/babel",
    install_source_map_support = False,
    node_modules = "@npm_deps//:node_modules",
)

# Compile source files with babel
genrule(
    name = "piled_src",
    srcs = [
        ":src_files",
    ],
    outs = ["src"],
    cmd = "$(location babel) src  --out-dir $@",
    tools = [":babel"],
)

Note that in this case src in cmd = "$(location babel) src --out-dir $@" is a folder in the :src_files filegroup.

filegroup(
    name = "src_files",
    srcs = glob([
        "src/**/*",
        ...
    ]),
)

After this it was unnecessary to use npm start, just used default node. I could just do

nodejs_binary(
    name = "app",
    data = [":piled_src"],
    entry_point = "workspace_name/src/server.js",
    node_modules = "@npm_deps//:node_modules",
)

本文标签: javascriptBazel How do i use nodeJSbinary rule to do quotnpm run startquotStack Overflow