Bare Modules
Module support for JavaScript.
Usage
External Modules for Bare
Bare provides no standard library beyond the core JavaScript API available through the Bare
namespace. Instead, we maintain a comprehensive collection of external modules built specifically for Bare:
Packages
A package is a directory with a package.json
file.
Fields
"name"
"name"
The name of the package. This is used for addon resolution, self-referencing, and importing packages by name.
"version"
"version"
The current version of the package. This is used for addon resolution.
"type"
"type"
The module format used for .js
files. If not defined, .js
files are interpreted as CommonJS. If set to "module"
, .js
files are instead interpreted as ES modules.
"exports"
"exports"
The entry points of the package. If defined, only the modules explicitly exported by the package may be imported when importing the package by name.
Subpath exports
A package may define more than one entry point by declaring several subpaths with the main export being "."
:
When importing the package by name, require('my-package')
will resolve to <modules>/my-package/index.js
whereas require('my-package/submodule')
will resolve to <modules>/my-package/lib/submodule.js
.
Conditional exports
Conditional exports allow packages to provide different exports for different conditions, such as the loading method the importing module uses (e.g. require()
vs import
):
When importing the package by name, require('my-package')
will resolve to <modules>/my-package/index.cjs
whereas import 'my-package'
will resolve to <modules>/my-package/index.mjs
.
Similarly, conditional exports can be used to provide different entry points for different runtimes:
To provide a fallback for when no other conditions match, the "default"
condition can be declared:
The following conditions are supported, listed in order from most specific to least specific as conditions should be defined:
"import"
Matches when the package is loaded via import
or import()
.
"require"
Matches when the package is loaded via require()
.
"asset"
Matches when the package is loaded via require.asset()
.
"addon"
Matches when the package is loaded via require.addon()
.
"bare"
"node"
Matches for any Node.js environment.
"<platform>"
"<arch>"
"simulator"
Matches when Bare was compiled for a simulator, i.e. when Bare.simulator
is true
.
"default"
The fallback that always matches. This condition should always be last.
Export conditions are evaluated in the order they are defined in the "exports"
field. This means that less specific conditionals defined first will override more specific conditions define later. For example, the following will always call ./fallback.js
because "default"
always matches and is defined first.
This is why the general rule is that conditions should be from most specific to least specific when defined.
Self-referencing
Within a package, exports defined in the "exports"
field can be referenced by importing the package by name. For example, given the following package.json
...
...any module within my-package
may reference these entry points using either require('my-package')
or require('my-package/submodule')
.
Exports sugar
If a package defines only a single export, "."
, it may leave out the subpath entirely:
"imports"
"imports"
A private mapping for import specifiers within the package itself. Similar to "exports"
, the "imports"
field can be used to conditional import other packages within the package. But unlike "exports"
, "imports"
permits mapping to external packages.
The rules are otherwise analogous to the "exports"
field.
Subpath imports
Just like exports, subpaths can be used when importing a module internally.
Conditional imports
Adding conditional imports allows importing different packages based on the configured conditions. As an example:
When importing the package bar
as require('bar')
will resolve to ./baz.cjs
, but when importing with import('bar')
will resolve to ./baz.mjs
.
To provide a fallback for when no other conditions are met, the "default"
condition can be configured like so:
The following conditions are supported, listed in order from most specific to least specific as conditions should be defined:
"import"
Matches when the package is loaded via import
or import()
.
"require"
Matches when the package is loaded via require()
.
"asset"
Matches when the package is loaded via require.asset()
.
"addon"
Matches when the package is loaded via require.addon()
.
"bare"
"node"
Matches for any Node.js environment.
"<platform>"
"<arch>"
"simulator"
Matches when Bare was compiled for a simulator, ie when Bare.simulator
is true
.
"default"
The fallback that always matches. This condition should always be last.
The general rule is that conditions should be from most specific to least specific when defined.
#
Prefix
All import maps are private to the package and allow mapping to external packages. Entries in "imports"
may start with #
to disambiguate from external packages, but it is not required unlike in Node.js.
"engines"
"engines"
The "engines"
field defines the engine requirements of the package. During module resolution, the versions declared by Bare.versions
will be tested against the requirements declared by the package and resolution fail if they're not satisfied.
API
Module.constants.states
Module.constants.states
The flags for the current state of a module.
EVALUATED
The module has been evaluated.
SYNTHESIZED
The module named exports have been detected.
DESTROYED
The module has been unloaded.
Module.constants.types
Module.constants.types
SCRIPT
The module is a CommonJS module.
MODULE
The module is a ECMAScript module.
JSON
The module is a JSON file.
BUNDLE
ADDON
The module is a native addon.
BINARY
The module is a binary file.
TEXT
The module is a text file.
Module.protocol
Module.protocol
The default ModuleProtocol
class for resolving, reading and loading modules. See Protocols for usage.
Module.cache
Module.cache
The global cache of loaded modules.
const url = Module.resolve(specifier, parentURL[, options])
const url = Module.resolve(specifier, parentURL[, options])
Resolve the module specifier
relative to the parentURL
. specifier
is a string and parentURL
is a WHATWG URL
.
Options include:
const module = Module.load(url[, source][, options])
const module = Module.load(url[, source][, options])
Load a module with the provided url
. url
is a WHATWG URL
. If provided, the source
will be passed to the matching extension
for the url
.
Options include:
const url = Module.asset(specifier, parentURL[, options])
const url = Module.asset(specifier, parentURL[, options])
Get the asset URL by resolving specifier
relative to parentURL
. specifier
is a string and parentURL
is a WHATWG URL
.
Options include:
module.url
module.url
The WHATWG URL
instance for the module.
module.filename
module.filename
The pathname of the module.url
.
module.dirname
module.dirname
The directory name of the module.
module.type
module.type
The type of the module. See Module.constants.types
for possible values.
module.defaultType
module.defaultType
The assumed type of a module without a type
using an ambiguous extension, such as .js
. See Module.constants.types
for possible values.
module.cache
module.cache
A cache of loaded modules for this module. Defaults to Module.cache
.
module.main
module.main
The module representing the entry script where the program was launched.
module.exports
module.exports
The exports from the module.
module.imports
module.imports
The import map when the module was loaded.
module.resolutions
module.resolutions
A map of preresolved imports with keys being serialized parent URLs and values being "imports"
maps.
module.builtins
module.builtins
A map of builtin module specifiers mapped to the loaded module.
module.conditions
module.conditions
An array of conditions used to resolve dependencies while loading the module. See Conditional Exports for possible values.
module.protocol
module.protocol
The ModuleProtocol
class used for resolving, reading and loading modules. See Protocols.
module.destroy()
module.destroy()
Unloads the module.
CommonJS modules
require(specifier[, options])
require(specifier[, options])
Used to import JavaScript or JSON modules and local files. Relative paths such as ./
, ./foo
, ./bar/baz
, and ../foo
will be resolved against the directory named by __dirname
. POSIX style paths are resolved in an OS independent fashion, meaning that the examples above will work on Windows in the same way they would on POSIX systems.
Returns the exported module contents.
Options include:
require.main
require.main
The module representing the entry script where the program was launched. The same value as module.main
for the current module.
require.cache
require.cache
A cache of loaded modules for this module. The same value as module.cache
for the current module.
const path = require.resolve(specifier[, parentURL])
const path = require.resolve(specifier[, parentURL])
Use the internal machinery of require()
to resolve the specifier
string relative to the URL parentURL
and return the path string.
require.addon([specifier][, parentURL])
require.addon([specifier][, parentURL])
Also used to import modules but specifically loads only addon modules. specifier
is resolved relative to parentURL
using the addon resolution algorithm.
Returns the exported module contents.
A common pattern for writing an addon module is to use require.addon()
as the JavaScript module exports:
See bare-addon
for a template of building native addon modules.
require.addon.host
require.addon.host
Returns the string representation of the platform and architecture used when resolving addons with the pattern <platform>-<arch>[-simulator]
. Returns the same value as Bare.Addon.host
.
const path = require.addon.resolve([specifier][, parentURL])
const path = require.addon.resolve([specifier][, parentURL])
Resolve the specifier
string relative to the URL parentURL
as an addon and returns the path string. The specifier
is resolved using the addon resolution algorithm.
const path = require.asset(specifier[, parentURL])
const path = require.asset(specifier[, parentURL])
Resolve the specifier
relative to the parentURL
and return the path of the asset as a string.
Can be used to load assets, for example the following loads ./foo.txt
from the local files:
ECMAScript modules
import defaultExport, * as name, { export1, export2 as alias2, ... } from 'specifier' with { type: 'json' }
import defaultExport, * as name, { export1, export2 as alias2, ... } from 'specifier' with { type: 'json' }
The static import
declaration is used to import read-only live bindings that are exported by another module. The imported bindings are called live bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module. In brief, you can import what is exported from another module.
For more information on import
syntax, see MDN.
import.meta.url
import.meta.url
The string representation of the URL for the current module.
import.meta.main
import.meta.main
A boolean representing whether the current module is the entry script where the program was launched.
import.meta.cache
import.meta.cache
A cache of loaded modules for this module. The same value as module.cache
for the current module.
const href = import.meta.resolve(specifier[, parentURL])
const href = import.meta.resolve(specifier[, parentURL])
A module-relative resolution function which returns the URL string for the module. The specifier
is a string which is resolved relative to the parentURL
which is a WHATWG URL.
import.meta.addon([specifier][, parentURL])
import.meta.addon([specifier][, parentURL])
Also used to import modules but specifically loads only addon modules. specifier
is resolved relative to parentURL
using the addon resolution algorithm.
Returns the exported module contents.
import.meta.addon.host
import.meta.addon.host
Returns the string representation of the platform and architecture used when resolving addons with the pattern <platform>-<arch>[-simulator]
. Returns the same value as Bare.Addon.host
.
const href = import.meta.addon.resolve([specifier][, parentURL])
const href = import.meta.addon.resolve([specifier][, parentURL])
Resolve the specifier
string relative to the URL parentURL
as an addon and returns the URL string. The specifier
is resolved using the addon resolution algorithm.
const href = import.meta.asset(specifier[, parentURL])
const href = import.meta.asset(specifier[, parentURL])
Resolve the specifier
relative to the parentURL
and return the URL of the asset as a string.
Custom require()
require()
Creating a custom require allows one to create a preconfigured require()
. This can be useful in scenarios such as a Read-Evaluate-Print-Loop (REPL) where the parent URL is set to a directory so requiring relative paths to work correctly.
const require = Module.createRequire(parentURL[, options])
const require = Module.createRequire(parentURL[, options])
Options include:
Protocols
Protocols define how to resolve, access and load modules. Custom protocols can be defined to extend or replace how module are resolved and loaded to support things like loading modules via a Hyperdrive
.
const protocol = new Module.Protocol(methods, context = null)
const protocol = new Module.Protocol(methods, context = null)
Methods include:
Bundles
const bundle = new Module.Bundle()
const bundle = new Module.Bundle()
Last updated