LogoPear Docs
ReferencesBareModules

bare-module

Reference for bare-module: Bare's module system—CommonJS and ESM resolution, loading, and the require / import.meta surfaces.

stable

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-module

Usage

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:

OptionDescription
moduleThe module to become the referrer for the returned require(). Defaults to a new module instance created from parentURL with the same options.
referrerThe referring module.
typeThe type of the module. See Module.constants.types for possible values. Defaults to SCRIPT.
defaultTypeThe assumed type of a module without a type using an ambiguous extension such as .js. Inherited from referrer if defined, otherwise defaults to SCRIPT.
cacheA cache of loaded modules. Inherited from referrer if defined, otherwise defaults to Module.cache.
mainThe module representing the entry script where the program was launched.
protocolThe ModuleProtocol to use for resolving and loading. Defaults to referrer's protocol if defined, otherwise Module.protocol.
importsA default "imports" map applied to all specifiers. Same syntax and rules as "imports" in package.json.
resolutionsA map of preresolved imports with keys being serialized parent URLs and values being "imports" maps.
builtinsA map of builtin module specifiers to loaded modules.
conditionsThe 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)

MethodSignatureDescription
preresolve(specifier, parentURL) => stringPreprocesses the specifier and parent URL before the resolve algorithm runs.
postresolve(url) => stringProcesses 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) => booleanReturns whether the URL exists.
read(url) => string | BufferReturns the source code of a URL as a string or buffer.
addon(url) => URLPost-processes URLs for addons before postresolve().
asset(url) => URLPost-processes URLs for assets before postresolve().

Builds on bare-module-resolve, bare-module-lexer, bare-bundle, bare-path, and bare-url.

See also

On this page