bare-module
Reference for bare-module: Bare's module system—CommonJS and ESM resolution, loading, and the require / import.meta surfaces.
bare-module implements Bare's module system: CommonJS and ESM with bidirectional interop, native-addon and asset resolution, and the require / import.meta surfaces. It's the machinery behind bare's Module.load(). It's a native addon and requires Bare >=1.23.0 <1.29.0.
npm i bare-moduleUsage
const Module = require('bare-module')API
Module
const url = Module.resolve(specifier, parentURL[, options]) · Module.asset(specifier, parentURL[, options])
Resolve a module or asset specifier to a URL.
const module = Module.load(url[, source][, options])
Load (and cache) a module. The returned module exposes url, filename, dirname, type, defaultType, cache, main, exports, imports, resolutions, builtins, conditions, and protocol.
Module.constants · Module.protocol · Module.cache
Module state/type constants, the protocol table, and the global module cache.
require
require(specifier[, options]) plus require.main, require.cache, require.resolve(specifier[, parentURL]), require.addon([specifier][, parentURL]) (with require.addon.host and require.addon.resolve(...)), and require.asset(specifier[, parentURL]).
import / import.meta
Static and dynamic import (including import attributes, for example with { type: 'json' }), plus import.meta.url, import.meta.main, import.meta.cache, import.meta.dirname, import.meta.filename, import.meta.resolve(specifier[, parentURL]), and import.meta.addon([specifier][, parentURL]).
Module.createRequire
const require = Module.createRequire(parentURL[, options])
Creates a preconfigured require() bound to parentURL. Useful in REPL scenarios where the parent URL should be set to a directory so relative paths resolve correctly.
Options include:
| Option | Description |
|---|---|
module | The module to become the referrer for the returned require(). Defaults to a new module instance created from parentURL with the same options. |
referrer | The referring module. |
type | The type of the module. See Module.constants.types for possible values. Defaults to SCRIPT. |
defaultType | The assumed type of a module without a type using an ambiguous extension such as .js. Inherited from referrer if defined, otherwise defaults to SCRIPT. |
cache | A cache of loaded modules. Inherited from referrer if defined, otherwise defaults to Module.cache. |
main | The module representing the entry script where the program was launched. |
protocol | The ModuleProtocol to use for resolving and loading. Defaults to referrer's protocol if defined, otherwise Module.protocol. |
imports | A default "imports" map applied to all specifiers. Same syntax and rules as "imports" in package.json. |
resolutions | A map of preresolved imports with keys being serialized parent URLs and values being "imports" maps. |
builtins | A map of builtin module specifiers to loaded modules. |
conditions | The supported import conditions. "default" is always recognized. |
Module.Protocol
Protocols define how modules are resolved, accessed, and loaded. Custom protocols can extend or replace the default resolution and loading behaviour—for example, to load modules via a Hyperdrive.
const protocol = new Module.Protocol(methods, context = null)
| Method | Signature | Description |
|---|---|---|
preresolve | (specifier, parentURL) => string | Preprocesses the specifier and parent URL before the resolve algorithm runs. |
postresolve | (url) => string | Processes the resolved URL. Can be used to convert file paths, etc. |
resolve | *(specifier, parentURL, imports) => [URL] | A generator that resolves the specifier to a URL. |
exists | (url) => boolean | Returns whether the URL exists. |
read | (url) => string | Buffer | Returns the source code of a URL as a string or buffer. |
addon | (url) => URL | Post-processes URLs for addons before postresolve(). |
asset | (url) => URL | Post-processes URLs for assets before postresolve(). |
Related modules
Builds on bare-module-resolve, bare-module-lexer, bare-bundle, bare-path, and bare-url.
See also
bare-module-resolveandbare-module-traverse—the resolution and traversal algorithms underneath.- Bare runtime API—
Bare.Addon, the native-addon loaderrequire.addonbuilds on. - Bare modules—the full
bare-*catalog.