What is browser-resolve?
The browser-resolve npm package is designed to resolve module dependencies in a way that is compatible with how browsers resolve modules, as opposed to the Node.js resolution algorithm. This is particularly useful for bundling packages for use in the browser where the Node.js module resolution strategy (e.g., handling of the 'main' field in package.json) does not apply.
What are browser-resolve's main functionalities?
Resolving browser-specific module entry points
This feature allows developers to resolve the path to a module's browser-specific entry point, if specified in the module's package.json, instead of the Node.js entry point. This is useful for bundling modules for the browser.
var resolve = require('browser-resolve');
resolve('module-name', { filename: '/path/to/file.js' }, function (err, res) {
if (err) console.log(err);
console.log(res);
});
Other packages similar to browser-resolve
resolve
Similar to browser-resolve, the 'resolve' package is a module resolution library for Node.js, mimicking Node's require/resolution mechanism. The key difference is that 'resolve' focuses on Node.js environments, while 'browser-resolve' targets browser environments, taking into account browser-specific fields in package.json.
webpack
Webpack is a powerful module bundler that can resolve dependencies and modules for browser environments. Unlike browser-resolve, which is a simple resolution library, webpack offers a wide range of features including bundling, minification, and plugin support. Webpack's resolution mechanism is more complex and configurable, designed for comprehensive build processes.
browserify
Browserify is a tool that allows developers to use Node.js-style modules in the browser. It resolves dependencies and bundles modules together. While browser-resolve provides the resolution logic similar to what Browserify uses under the hood, Browserify offers a complete bundling solution, transforming Node.js modules so they can run in the browser.
browser-resolve
node.js resolve algorithm with browser field support.
example
relative
you can resolve files like require.resolve()
:
var resolve = require('browser-resolve');
resolve('../', { filename: __filename }, function(err, path) {
console.log(path);
});
$ node example/resolve.js
/home/substack/projects/node-browser-resolve/index.js
core
or if you require()
core modules you'll get a version that works in browsers:
var resolve = require('browser-resolve');
resolve('fs', null, function(err, path) {
console.log(path);
});
$ node example/builtin.js
/home/substack/projects/node-browser-resolve/builtin/fs.js
custom
and you can use the
browser field to load
browser-specific versions of modules:
{
"name": "custom",
"version": "0.0.0",
"browser": {
"./main.js": "custom.js"
}
}
var resolve = require('browser-resolve');
var parent = { filename: __dirname + '/custom/file.js' };
resolve('./main.js', parent, function(err, path) {
console.log(path);
});
$ node example/custom.js
/home/substack/projects/node-browser-resolve/example/custom/custom.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 resolve = require('browser-resolve');
var parent = { filename: __dirname + '/skip/main.js' };
resolve('tar', parent, function(err, path) {
console.log(path);
});
$ node example/skip.js
/home/substack/projects/node-browser-resolve/empty.js
license
MIT