implements the node require.resolve() algorithm such that you can require.resolve() on behalf of a file asynchronously and synchronously
asynchronously resolve:
varresolve=require('resolve/async');// or, require('resolve')resolve('tap',{basedir: __dirname},function(err,res){if(err)console.error(err);elseconsole.log(res);});$ node example/async.js /home/substack/projects/node-resolve/node_modules/tap/lib/main.js synchronously resolve:
varresolve=require('resolve/sync');// or, `require('resolve').syncvarres=resolve('tap',{basedir: __dirname});console.log(res);$ node example/sync.js /home/substack/projects/node-resolve/node_modules/tap/lib/main.js varresolve=require('resolve');varasync=require('resolve/async');varsync=require('resolve/sync');For both the synchronous and asynchronous methods, errors may have any of the following err.code values:
MODULE_NOT_FOUND: the given path string (id) could not be resolved to a moduleINVALID_BASEDIR: the specifiedopts.basedirdoesn't exist, or is not a directoryINVALID_PACKAGE_MAIN: apackage.jsonwas encountered with an invalidmainproperty (eg. not a string)
Asynchronously resolve the module path string id into cb(err, res [, pkg]), where pkg (if defined) is the data from package.json.
options are:
opts.basedir - directory to begin resolving from
opts.package -
package.jsondata applicable to the module being loadedopts.extensions - array of file extensions to search in order
opts.includeCoreModules - set to
falseto exclude node core modules (e.g.fs) from the searchopts.readFile - how to read files asynchronously
opts.isFile - function to asynchronously test whether a file exists
opts.isDirectory - function to asynchronously test whether a file exists and is a directory
opts.realpath - function to asynchronously resolve a potential symlink to its real path
opts.readPackage(readFile, pkgfile, cb)- function to asynchronously read and parse a package.json file- readFile - the passed
opts.readFileorfs.readFileif not specified - pkgfile - path to package.json
- cb - callback
- readFile - the passed
opts.packageFilter(pkg, pkgfile, dir)- transform the parsed package.json contents before looking at the "main" field- pkg - package data
- pkgfile - path to package.json
- dir - directory that contains package.json
opts.pathFilter(pkg, path, relativePath)- transform a path within a package- pkg - package data
- path - the path being resolved
- relativePath - the path relative from the package.json location
- returns - a relative path that will be joined from the package.json location
opts.paths - require.paths array to use if nothing is found on the normal
node_modulesrecursive walk (probably don't use this)For advanced users,
pathscan also be aopts.paths(request, start, opts)function- request - the import specifier being resolved
- start - lookup path
- getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard
node_modulesresolution - opts - the resolution options
opts.packageIterator(request, start, opts)- return the list of candidate paths where the packages sources may be found (probably don't use this)- request - the import specifier being resolved
- start - lookup path
- getPackageCandidates - a thunk (no-argument function) that returns the paths using standard
node_modulesresolution - opts - the resolution options
opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default:
"node_modules"opts.preserveSymlinks - if true, doesn't resolve
basedirto real path before resolving. This is the way Node resolves dependencies when executed with the --preserve-symlinks flag. Note: this property is currentlytrueby default but it will be changed tofalsein the next major version because Node's resolution algorithm does not preserve symlinks by default.
default opts values:
{paths: [],basedir: __dirname,extensions: ['.js'],includeCoreModules: true,readFile: fs.readFile,isFile: functionisFile(file,cb){fs.stat(file,function(err,stat){if(!err){returncb(null,stat.isFile()||stat.isFIFO());}if(err.code==='ENOENT'||err.code==='ENOTDIR')returncb(null,false);returncb(err);});},isDirectory: functionisDirectory(dir,cb){fs.stat(dir,function(err,stat){if(!err){returncb(null,stat.isDirectory());}if(err.code==='ENOENT'||err.code==='ENOTDIR')returncb(null,false);returncb(err);});},realpath: functionrealpath(file,cb){varrealpath=typeoffs.realpath.native==='function' ? fs.realpath.native : fs.realpath;realpath(file,function(realPathErr,realPath){if(realPathErr&&realPathErr.code!=='ENOENT')cb(realPathErr);elsecb(null,realPathErr ? file : realPath);});},readPackage: functiondefaultReadPackage(readFile,pkgfile,cb){readFile(pkgfile,function(readFileErr,body){if(readFileErr)cb(readFileErr);else{try{varpkg=JSON.parse(body);cb(null,pkg);}catch(jsonErr){cb(null);}}});},moduleDirectory: 'node_modules',preserveSymlinks: true}Synchronously resolve the module path string id, returning the result and throwing an error when id can't be resolved.
options are:
opts.basedir - directory to begin resolving from
opts.extensions - array of file extensions to search in order
opts.includeCoreModules - set to
falseto exclude node core modules (e.g.fs) from the searchopts.readFileSync - how to read files synchronously
opts.isFile - function to synchronously test whether a file exists
opts.isDirectory - function to synchronously test whether a file exists and is a directory
opts.realpathSync - function to synchronously resolve a potential symlink to its real path
opts.readPackageSync(readFileSync, pkgfile)- function to synchronously read and parse a package.json file- readFileSync - the passed
opts.readFileSyncorfs.readFileSyncif not specified - pkgfile - path to package.json
- readFileSync - the passed
opts.packageFilter(pkg, dir)- transform the parsed package.json contents before looking at the "main" field- pkg - package data
- dir - directory that contains package.json (Note: the second argument will change to "pkgfile" in v2)
opts.pathFilter(pkg, path, relativePath)- transform a path within a package- pkg - package data
- path - the path being resolved
- relativePath - the path relative from the package.json location
- returns - a relative path that will be joined from the package.json location
opts.paths - require.paths array to use if nothing is found on the normal
node_modulesrecursive walk (probably don't use this)For advanced users,
pathscan also be aopts.paths(request, start, opts)function- request - the import specifier being resolved
- start - lookup path
- getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard
node_modulesresolution - opts - the resolution options
opts.packageIterator(request, start, opts)- return the list of candidate paths where the packages sources may be found (probably don't use this)- request - the import specifier being resolved
- start - lookup path
- getPackageCandidates - a thunk (no-argument function) that returns the paths using standard
node_modulesresolution - opts - the resolution options
opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default:
"node_modules"opts.preserveSymlinks - if true, doesn't resolve
basedirto real path before resolving. This is the way Node resolves dependencies when executed with the --preserve-symlinks flag. Note: this property is currentlytrueby default but it will be changed tofalsein the next major version because Node's resolution algorithm does not preserve symlinks by default.
default opts values:
{paths: [],basedir: __dirname,extensions: ['.js'],includeCoreModules: true,readFileSync: fs.readFileSync,isFile: functionisFile(file){try{varstat=fs.statSync(file);}catch(e){if(e&&(e.code==='ENOENT'||e.code==='ENOTDIR'))returnfalse;throwe;}returnstat.isFile()||stat.isFIFO();},isDirectory: functionisDirectory(dir){try{varstat=fs.statSync(dir);}catch(e){if(e&&(e.code==='ENOENT'||e.code==='ENOTDIR'))returnfalse;throwe;}returnstat.isDirectory();},realpathSync: functionrealpathSync(file){try{varrealpath=typeoffs.realpathSync.native==='function' ? fs.realpathSync.native : fs.realpathSync;returnrealpath(file);}catch(realPathErr){if(realPathErr.code!=='ENOENT'){throwrealPathErr;}}returnfile;},readPackageSync: functiondefaultReadPackageSync(readFileSync,pkgfile){varbody=readFileSync(pkgfile);try{varpkg=JSON.parse(body);returnpkg;}catch(jsonErr){}},moduleDirectory: 'node_modules',preserveSymlinks: true}With npm do:
npm install resolveMIT
