The most important class that is exposed by Components.js is the Loader, which essentially does only two things:

  1. It accepts module and component registrations, so that Components.js knows how to instantiate components.
  2. It instantiates components based on configurations.

There are however different ways to register modules and their components, and different ways to instantiate these components from configurations.

This is an example of registering and instantiating by URL:

const Loader = require('componentsjs').Loader;

const loader = new Loader();
await loader.registerModuleResourcesUrl('path/or/url/to/my-module.jsonld');
const myComponent = await loader.instantiateFromUrl(
    'http://example.org/myInstance', 'path/or/url/to/config-my-component.jsonld');

Example Source

Note

Loader has a strict registration and instantiation phase. This means that all registrations should happen before instantiations. Any registrations being done after an instantiation has happened will result in a runtime exception.

This is needed because the Loader creates internal caches when the first instantiation is done to increase the efficiency of all following instantiations.

Loader Options

The Loader can be constructed with an optional options hash. This hash accepts the following optional entries:

Key Value Default
scanGlobal If Components.js modules and JSON-LD contexts should be scanned in all globally installed NPM modules as well, instead of only the locally installed modules. false
absolutizeRelativePaths If component configurations with relative file path strings ("file://...") should be made absolute to the main module path ("file:///..."). true
contexts A hash with JSON-LD contexts. Entries with key a JSON-LD context URI and values a string representation of a JSON-LD context. Collected from all scanned modules.
mainModulePath Path that should be considered the root Node module. Everything will happen relative to this path. The path of the closest NPM module, which is determined by iteratively checking the parents of the Node working directory.