What is @rollup/plugin-node-resolve?
The @rollup/plugin-node-resolve package is a plugin for Rollup that allows it to resolve node_modules on your behalf, similar to how Node.js would work. It helps in bundling packages with Rollup by locating and bundling third-party dependencies from the node_modules directory.
What are @rollup/plugin-node-resolve's main functionalities?
Resolving modules from node_modules
Automatically resolves modules from the node_modules directory, allowing you to import dependencies in your project.
import resolve from '@rollup/plugin-node-resolve';
export default {
plugins: [
resolve()
]
};
Custom resolution for specific modules
Allows you to specify custom resolution options for modules, such as looking in different directories.
import resolve from '@rollup/plugin-node-resolve';
export default {
plugins: [
resolve({
customResolveOptions: {
moduleDirectory: 'custom_modules'
}
})
]
};
Browser field support for package.json
Respects the 'browser' field in package.json when bundling packages, which can specify alternative files to load for certain environments.
import resolve from '@rollup/plugin-node-resolve';
export default {
plugins: [
resolve({
browser: true
})
]
};
Main fields configuration
Allows configuration of which fields in package.json files it should look at when trying to resolve an import.
import resolve from '@rollup/plugin-node-resolve';
export default {
plugins: [
resolve({
mainFields: ['module', 'main']
})
]
};
Other packages similar to @rollup/plugin-node-resolve
webpack
Webpack is a powerful module bundler that comes with its own resolution mechanism similar to @rollup/plugin-node-resolve. It resolves modules by default and can be configured extensively via its configuration file.
browserify
Browserify is another bundler that allows you to require('modules') in the browser by bundling up all of your dependencies. It also has a resolution mechanism similar to Node.js, much like @rollup/plugin-node-resolve.
parcel-bundler
Parcel is a web application bundler that has zero configuration by default and includes a powerful module resolution system that works out of the box, similar to @rollup/plugin-node-resolve.