browser-resolve
resolve which handles browser field support in package.json
Last updated 12 years ago by shtylman .
MIT · Repository · Original npm · Tarball · package.json
$ npm install browser-resolve 
SYNC missed versions from official npm registry.

browser-resolve Build Status

node.js resolve algorithm with browser field support.

api

bresolve(id, opts={}, cb)

Resolve a module path and call cb(err, path [, pkg])

Options:

  • basedir - directory to begin resolving from
  • browser - the 'browser' property to use from package.json (defaults to 'browser')
  • filename - the calling filename where the require() call originated (in the source)
  • modules - object with module id/name -> path mappings to consult before doing manual resolution (use to provide core modules)
  • packageFilter - transform the parsed package.json contents before looking at the main field
  • paths - require.paths array to use if nothing is found on the normal node_modules recursive walk

Additionally, options supported by node-resolve can be used.

bresolve.sync(id, opts={})

Same as the async resolve, just uses sync methods.

Additionally, options supported by node-resolve can be used.

basic usage

you can resolve files like require.resolve():

var bresolve = require('browser-resolve');
bresolve('../', { filename: __filename }, function(err, path) {
    console.log(path);
});
$ node example/resolve.js
/home/substack/projects/browser-resolve/index.js

core modules

By default, core modules (http, dgram, etc) will return their same name as the path. If you want to have specific paths returned, specify a modules property in the options object.

var shims = {
    http: '/your/path/to/http.js'
};

var bresolve = require('browser-resolve');
bresolve('http', { modules: shims }, function(err, path) {
    console.log(path);
});
$ node example/builtin.js
/home/substack/projects/browser-resolve/builtin/http.js

browser field

browser-specific versions of modules

{
  "name": "custom",
  "version": "0.0.0",
  "browser": {
    "./main.js": "custom.js"
  }
}
var bresolve = require('browser-resolve');
var parent = { filename: __dirname + '/custom/file.js' };
bresolve('./main.js', parent, function(err, path) {
    console.log(path);
});
$ node example/custom.js
/home/substack/projects/browser-resolve/example/custom/custom.js

You can use different package.json properties for the resolution, if you want to allow packages to target different environments for example:

{
  "browser": { "./main.js": "custom.js" },
  "chromeapp": { "./main.js": "custom-chromeapp.js" }
}
var bresolve = require('browser-resolve');
var parent = { filename: __dirname + '/custom/file.js', browser: 'chromeapp' };
bresolve('./main.js', parent, function(err, path) {
    console.log(path);
});
$ node example/custom.js
/home/substack/projects/browser-resolve/example/custom/custom-chromeapp.js

skip

You can skip over dependencies by setting a browser field value to false:

{
  "name": "skip",
  "version": "0.0.0",
  "browser": {
    "tar": false
  }
}

This is handy if you have code like:

var tar = require('tar');

exports.add = function (a, b) {
    return a + b;
};

exports.parse = function () {
    return tar.Parse();
};

so that require('tar') will just return {} in the browser because you don't intend to support the .parse() export in a browser environment.

var bresolve = require('browser-resolve');
var parent = { filename: __dirname + '/skip/main.js' };
bresolve('tar', parent, function(err, path) {
    console.log(path);
});
$ node example/skip.js
/home/substack/projects/browser-resolve/empty.js

license

MIT

upgrade notes

Prior to v1.x this library provided shims for node core modules. These have since been removed. If you want to have alternative core modules provided, use the modules option when calling bresolve().

This was done to allow package managers to choose which shims they want to use without browser-resolve being the central point of update.

Current Tags

  • 2.0.0                                ...           latest (4 years ago)

42 Versions

  • 2.0.0                                ...           4 years ago
  • 1.11.3                                ...           6 years ago
  • 1.11.2                                ...           8 years ago
  • 1.11.1                                ...           9 years ago
  • 1.11.0                                ...           9 years ago
  • 1.10.1                                ...           9 years ago
  • 1.10.0                                ...           9 years ago
  • 1.9.1                                ...           9 years ago
  • 1.9.0                                ...           10 years ago
  • 1.8.2                                ...           10 years ago
  • 1.8.1                                ...           10 years ago
  • 1.8.0                                ...           10 years ago
  • 1.7.2                                ...           10 years ago
  • 1.7.1                                ...           10 years ago
  • 1.7.0                                ...           10 years ago
  • 1.6.0                                ...           10 years ago
  • 1.5.0                                ...           10 years ago
  • 1.4.1                                ...           10 years ago
  • 1.4.0                                ...           10 years ago
  • 1.3.2                                ...           10 years ago
  • 1.3.1                                ...           10 years ago
  • 1.3.0                                ...           10 years ago
  • 1.2.4                                ...           11 years ago
  • 1.2.3                                ...           11 years ago
  • 1.2.2                                ...           11 years ago
  • 1.2.1                                ...           11 years ago
  • 1.2.0                                ...           11 years ago
  • 1.1.4                                ...           11 years ago
  • 1.1.3                                ...           11 years ago
  • 1.1.2                                ...           11 years ago
  • 1.1.1                                ...           11 years ago
  • 1.1.0                                ...           11 years ago
  • 1.0.1                                ...           12 years ago
  • 1.0.0                                ...           12 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
  • 0.0.5                                ...           12 years ago
  • 0.0.4                                ...           12 years ago
  • 0.0.3                                ...           12 years ago
  • 0.0.2                                ...           12 years ago
  • 0.0.1                                ...           12 years ago
  • 0.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 28
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE