Reflection helpers for JavaScript projects: dynamically import a script file and look up or call its exported objects, functions and classes.
Note: This repository is archived and read-only.
Package @ralvarezdev/js-reflection (0.3.11, ES modules, no dependencies).
npm publication was not verified; installing from GitHub works regardless:
npm install github:ralvarezdev/js-reflectionimport Script from "@ralvarezdev/js-reflection";
const script = new Script("./path/to/module.js");
const value = await script.getObject("someExport");
const result = await script.callFunction("someFunction", 1, 2);
const instance = await script.callNew("SomeClass");Exported from index.js: default Script, isClass, isFunction.
new Script(scriptPath) adds a .js extension if missing and imports the script lazily, with caching. Async methods:
- Lookup —
loadedScript(),getObject(name),getObjectProperty(obj, prop),getObjectFunctionProperty(obj, prop),getNestedObjectProperty(obj, ...props),getFunction(name),getClass(name),getClassMethods(className) - Calls —
callFunction(name, ...params),callObjectMethod(obj, method, ...params),callNew(className, ...params) - Safe calls —
safeCallFunction,safeCallObjectMethod,safeCallNewthrowMismatched number of parametersif the argument count differs from the function's arity.
Missing paths, objects or properties and wrong kinds throw errors such as Object not found or Object is not a class.
Tests use Node's built-in runner (index.test.js, load/index.test.js, load/script.test.js):
npm test # node index.test.jsindex.js
load/ script.js (Script), helpers.js (isClass, isFunction), index.js, *.test.js
GNU General Public License v3.0. package.json declares GPL-3.0-only, matching the LICENSE file.