Modules are the preferred way to expose application APIs to scripts.
ScriptModuleExportResolverFactory getModuleExportResolver();
void setModuleExportResolver(ScriptModuleExportResolverFactory factory);
ScriptAccess getScriptAccess();
void setScriptAccess(ScriptAccess scriptAccess);
ScriptInternalModule getGlobalModule();
Map<String, ScriptModule> getModules();
ScriptModule getModule(String name);
void addModule(ScriptModule module);
void removeModule(String name);Every runtime has its own module manager implementation.
| Type | Interface | Description |
|---|---|---|
| Internal | ScriptInternalModule |
Java-defined exports. |
| External | ScriptExternalModule |
JavaScript source loaded from file or bytes. |
SimpleScriptInternalModule app = new SimpleScriptInternalModule("app");
app.export(new ScriptValueExport("version", "1.0.0"));
script.getModuleManager().addModule(app);import { version } from "app";
version;Exports in getGlobalModule() are available without import.
Runtime implementations also copy globally registered functions and constants into the global module before compilation.
Use global exports sparingly. Modules are clearer for larger APIs.
| Export | Description |
|---|---|
ScriptFunctionExport |
Exports a ScriptFunction; runtime creates a callable wrapper. |
ScriptFunctionDefinitionExport |
Exports an existing function definition. |
ScriptConstantExport |
Exports a ScriptConstant. |
ScriptValueExport |
Exports a Java value, object instance, enum, or Class<?>. |
module.export(new ScriptValueExport("config", Map.of("mode", "dev")));
module.export(new ScriptValueExport("Service", UserService.class));
module.export(new ScriptValueExport("service", new UserService()));Access to Java members depends on ScriptAccess.
ScriptInternalModule.copy copies exports from another internal module.
SimpleScriptInternalModule std = new SimpleScriptInternalModule("std");
std.copy(new StandardScriptModule());
script.getModuleManager().addModule(std);Copy rejects conflicting exports with the same name and different hash.
GraalVM supports native module imports through its Scriptify virtual file system.
Rhino supports these top-level forms:
import * as name from "module";
import { value, other as alias } from "module";
import defaultValue from "module";
import "module";For runtime-specific details, see Runtime Selection and External Modules.