Lux.NodeJS (Lux v0.5.0)

View Source

Provides functions for executing Node.js code with variable bindings.

The ~JS sigil is used to write Node.js code directly in Elixir files. In the Node.js code, you have to export a function named main that takes an object as an argument and returns a value.

export const main = ({x, y}) => x + y

Examples

iex> require Lux.NodeJS
iex> Lux.NodeJS.nodejs variables: %{x: 40, y: 2} do
...>   ~JS'''
...>   export const main = ({x, y}) => x + y
...>   '''
...> end
42

Summary

Functions

Evaluates Node.js code with optional variable bindings and other options.

Same as eval/2, but raises an error.

Attempts to import a Node.js package. Currently, it will modify priv/node/package.json and priv/node/package_lock.json files.

Returns a main module path for the Node.js.

A macro for executing Node.js code with variable bindings. Node.js code should be wrapped in a sigil ~JS to bypass Elixir syntax checking.

Types

eval_option()

@type eval_option() :: {:variables, map()} | {:timeout, pos_integer()}

eval_options()

@type eval_options() :: [eval_option()]

import_result()

@type import_result() :: %{required(String.t()) => boolean() | String.t()}

Functions

child_spec(opts \\ [])

@spec child_spec(keyword()) :: :supervisor.child_spec()

eval(code, opts \\ [])

@spec eval(String.t(), eval_options()) :: {:ok, term()} | {:error, String.t()}

Evaluates Node.js code with optional variable bindings and other options.

Options

  • :variables - A map of variables to bind in the Node.js context
  • :timeout - Timeout in milliseconds for Node.js execution

Examples

iex> Lux.NodeJS.eval("export const main = ({x}) => x * 2", variables: %{x: 21})
{:ok, 42}

iex> Lux.NodeJS.eval("export const main = () => os.getenv('TEST')", env: %{"TEST" => "value"})
{:ok, "value"}

eval!(code, opts \\ [])

Same as eval/2, but raises an error.

import_package(package_name, opts \\ [])

@spec import_package(
  String.t(),
  keyword()
) :: {:ok, import_result()} | {:error, String.t()}

Attempts to import a Node.js package. Currently, it will modify priv/node/package.json and priv/node/package_lock.json files.

Options

  • :update_lock_file - Whether to update the lock file after importing the package (default: true)
  • :timeout - Timeout in milliseconds for Node.js execution

module_path()

@spec module_path() :: String.t()

Returns a main module path for the Node.js.

nodejs(opts \\ [], list)

(macro)

A macro for executing Node.js code with variable bindings. Node.js code should be wrapped in a sigil ~JS to bypass Elixir syntax checking.