rules_js icon indicating copy to clipboard operation
rules_js copied to clipboard

How to set env for nextjs ?

Open antspy opened this issue 6 months ago • 1 comments

Hi,

I have a nextJS app and using the nextjs macros (thank you so much for adding these btw! )

nextjs(
    name = "next",
    srcs = [
        "//src/app",
    ],
    config = "next.config.mjs",
    data = [
        "//:node_modules/next",
        "//:node_modules/react",
        "//:node_modules/react-dom",
        "//:node_modules/styled-jsx",
        "//:node_modules/typescript",
        "//:package_json",
    ],
    next_js_binary = "//:next_js_binary",
)

This works perfectly fine when I do bazel run next.dev. However, if I do bazel run next.start, I have issues when nextJS tries to pre-render the page (e.g. /). Specifically, some required environment variables are not set.

How do I make sure that the env variables are set (i.e. available in process.env) when nextJS is pre-rendering some routes during next.start?


I tried playing around with passing env = {'MY_VAR': 'xxx'} in the nextjs rule, and that works, but 'xxx' must be hardcoded, so that I cannot really use it (MY_VAR can be some API token, for example, so I cannot hardcode it into the BUILD file).

I tried also using bazel run --action_env=MY_VAR next.start but here it's also not able to find the MY_VAR definition.

What is the recommended approach here?

Thanks a lot for your help!

antspy avatar Jun 06 '25 10:06 antspy

Perhaps @jbedard knows?

thesayyn avatar Jun 12 '25 18:06 thesayyn