Lux.NodeJS (Lux v0.5.0)
View SourceProvides 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
@type eval_option() :: {:variables, map()} | {:timeout, pos_integer()}
@type eval_options() :: [eval_option()]
Functions
@spec child_spec(keyword()) :: :supervisor.child_spec()
@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"}
Same as eval/2
, but raises an error.
@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
@spec module_path() :: String.t()
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.