Package Exports
- cjs-module/resolve
- cjs-module/resolve/sync
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cjs-module) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
cjs-module
CJS (Node.js) modules resolver
Environment agnostic CJS (Node.js) modules resolver.
It implements a strict version of Node.js modules resolution logic, differences are as follows:
- Loading from global folders is not supported
- Only Unix path separators (
/
) are supported in require's path arguments - There's no awareness of node.js core modules
e.g.
resolve(dir, 'fs')
will naturally result with null
Installation
$ npm install cjs-module
API
getResolver(extensions, confirmFile, resolvePackageMain)
For provided configuration, returns a CJS modules resolver:
- extensions - List of supported file extensions in order of singificance, e.g. for Node.js it would be
['.js', '.json', '.node']
- confirmFile - a
confirmFile(filepath)
function. Confirms whether there's a module at provided (not normalized, absolute) file path. Returns promise-like object which resolves with either normalized full path of a module or null (if there's no module for given path).
Although result is expected to be a promise-like object, resolution can be synchronous. - resolvePackageMain - a
resolvePackageMain(dirpath)
function. Returns value of package.json'smain
property for given path. Returns promise-like object which resolves with either resolved value, or null, when eitherpackage.json
file was not found, or it didn't have main property.
Same as withconfirmFile
resolution can be synchronous.
resolve(dir, path)
Node.js resolver
Asynchronously resolves module path against provided directory path.
Returns promise. If no matching module was found, promise resolves with null
otherwise
full module path becomes a resolved value.
var resolve = require('cjs-module/resolve')
// Asynchronously resolve path for 'foo' module against current path
resolve(__dirname, 'foo').done(function (fooModulePath) {
if (!fooModulePath) {
// 'foo' module doesn't exist
} else {
// 'foo' module found at fooModulePath
}
})
resolveSync(dir, path)
Node.js resolver
Synchronously resolves module path against provided directory path.
If matching module was found then full module path is returned, otherwise null
.
var resolveSync = require('cjs-module/resolve/sync')
// Synchronously resolve path for 'foo' module against current path
var fooModulePath = resolveSync(__dirname, 'foo');
if (!fooModulePath) {
// 'foo' module doesn't exist
} else {
// 'foo' module found
}
Tests 
$ npm test