Skip to content

Use Native ES6 Modules #228

Closed
Closed

Description

@PalanQu

Hi, I want to use native ES6 modules in my project, But when I add the
templated_args = ["--node_options=--experimental-modules"] in my nodejs_binary rule, Bazel ignore my code and do nothing during running bazel run :env_test

Here is my environment
bazel version : Build label: 0.14.0-homebrew
rules_nodejs : 0.10.0
node -v : v10.4.0
yarn -v : 1.7.0
os : macos 10.12.6

WORKSPACE

workspace(name = "hello_example")

git_repository(
    name = "build_bazel_rules_nodejs",
    remote = "https://github.com/bazelbuild/rules_nodejs.git",
    tag = "0.10.0", # check for the latest tag when you install
)
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")

node_repositories(
  node_path = "/usr/local",
  yarn_path = "/usr/local/Cellar/yarn/1.7.0/libexec",
  package_json = ["//:package.json"]
)
load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")

yarn_install(
    name = "node_yarn",
    package_json = "//:package.json",
    yarn_lock = "//:yarn.lock",
)

BUILD.bazel

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

nodejs_binary(
    name = "env_test",
    node_modules = "@node_yarn//:node_modules",
    data = [
        "env.mjs"
    ],
    entry_point = "hello_example/env.mjs",
    templated_args = ["--node_options=--experimental-modules"]
)

env.mjs

import dayjs from 'dayjs'
var daytime = dayjs().format();
console.log(daytime);

bazel run :env_test

INFO: Build options have changed, discarding analysis cache.
INFO: Analysed target //:env_test (12 packages loaded).
INFO: Found 1 target...
Target //:env_test up-to-date:
  bazel-bin/env_test_bin.sh
  bazel-bin/env_test
INFO: Elapsed time: 0.701s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
(node:7763) ExperimentalWarning: The ESM module loader is experimental.
WARNING: source-map-support module not installed.
   Stack traces from languages like TypeScript will point to generated .js files.

I have checked bazel-bin/env_test, There is nothing about my code
Thanks all!

Activity

  1. alexeagle commented on Jun 13, 2018

    @alexeagle
    Collaborator

    Can you explain why you want to use native modules? Is it just an experiment, or is there something you need to do which isn't possible otherwise?

    (node:7763) ExperimentalWarning: The ESM module loader is experimental. looks like your option was passed to node and was used. But our loader didn't execute the entry point script.

    You can debug this by reading/modifying bazel-bin/env_test_bin_loader.js - I find that require.main is undefined - see nodejs/node#21143 - there isn't a concept of a "main" under this experimental option. Seems not ready for usage...

  2. PalanQu commented on Jun 13, 2018

    @PalanQu
    Author

    @alexeagle Because we have writen many code build by npm in ES6 modules, We want to use bazel to build our project, So we test if our code can transfer to build by bazel in a simple way. Many thx for your reply.

  3. alexeagle commented on Sep 10, 2019

    @alexeagle
    Collaborator

    Update: we now use ES modules in nodejs code, like the new Kotlin example:

    https://github.com/bazelbuild/rules_nodejs/blob/14003a0a0f65eb9a596e8e168d6e6b3b41997d6f/examples/kotlin/BUILD.bazel#L59

    It doesn't use require.main and works fine. Please open another issue if there's something specific broken about using --experimental-modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions