Resolve files from node modules
n-resolve
✂️ Resolve any file type fromnode_modules
😱 Promises
Installation
Grab it from npm
npm install n-resolve
Usage
const nResolve = require('n-resolve');
nResolve('n-resolve')
.then(resolved => {
console.log(resolved);
// => node_modules/n-resolve/distribution/index.js
})
.catch(error => {
throw error;
});
API
nResolve
Resolve a node module
Parameters
Options
Options
Properties
base
The base path to resolve from, defaults to process.cwd()
Examples
nResolve('n-resolve', {base: '/usr/local/lib/'})
.then(result => console.log(result));
// => /usr/local/lib/node_modules/distribution/index.js
.catch(error => {throw error});
index
The default index basename to use, defaults to 'index.js'
Examples
nResolve('normalize.css', {index: 'index.css'})
.then(result => console.log(result));
// => node_modules/normalize.css/index.css
.catch(error => {throw error});
modules
Map of module names to custom resolve paths
Examples
nResolve('events', {modules: {events: './foo.js'}})
.then(result => console.log(result));
// => ./foo.js
.catch(error => {throw error});
resolveKey
Resolve key to use, defaults to 'main'
Examples
nResolve('isomorphic-fetch')
.then(result => console.log(result));
// => node_modules/isomorphic-fetch/fetch-npm-node.js
.catch(error => {throw error});
nResolve('isomorphic-fetch', {resolveKey: 'browser'})
.then(result => console.log(result));
// => node_modules/isomorphic-fetch/fetch-npm-browserify.js
.catch(error => {throw error});
See also
Acknowledgements
The basic concept is taken from browser-resolve, as well as the tests and fixtures used for automated testing. Notable differences to browser-resolve are:
- No
paths
parameter - Does not default to
browser
field - No support for object mappings in
browser
field - Renamed
browser
option toresolveKey
- More details in error messages
Built by (c) marionebl. Released under the MIT license.