| 1 | [npm]: https://img.shields.io/npm/v/@rollup/plugin-node-resolve
|
|---|
| 2 | [npm-url]: https://www.npmjs.com/package/@rollup/plugin-node-resolve
|
|---|
| 3 | [size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-node-resolve
|
|---|
| 4 | [size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-node-resolve
|
|---|
| 5 |
|
|---|
| 6 | [![npm][npm]][npm-url]
|
|---|
| 7 | [![size][size]][size-url]
|
|---|
| 8 | [](https://liberamanifesto.com)
|
|---|
| 9 |
|
|---|
| 10 | # @rollup/plugin-node-resolve
|
|---|
| 11 |
|
|---|
| 12 | 🍣 A Rollup plugin which locates modules using the [Node resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together), for using third party modules in `node_modules`
|
|---|
| 13 |
|
|---|
| 14 | ## Requirements
|
|---|
| 15 |
|
|---|
| 16 | This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
|
|---|
| 17 |
|
|---|
| 18 | ## Install
|
|---|
| 19 |
|
|---|
| 20 | Using npm:
|
|---|
| 21 |
|
|---|
| 22 | ```console
|
|---|
| 23 | npm install @rollup/plugin-node-resolve --save-dev
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | ## Usage
|
|---|
| 27 |
|
|---|
| 28 | Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|---|
| 32 |
|
|---|
| 33 | export default {
|
|---|
| 34 | input: 'src/index.js',
|
|---|
| 35 | output: {
|
|---|
| 36 | dir: 'output',
|
|---|
| 37 | format: 'cjs'
|
|---|
| 38 | },
|
|---|
| 39 | plugins: [nodeResolve()]
|
|---|
| 40 | };
|
|---|
| 41 | ```
|
|---|
| 42 |
|
|---|
| 43 | Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
|
|---|
| 44 |
|
|---|
| 45 | ## Package entrypoints
|
|---|
| 46 |
|
|---|
| 47 | This plugin supports the package entrypoints feature from node js, specified in the `exports` or `imports` field of a package. Check the [official documentation](https://nodejs.org/api/packages.html#packages_package_entry_points) for more information on how this works. This is the default behavior. In the abscence of these fields, the fields in `mainFields` will be the ones to be used.
|
|---|
| 48 |
|
|---|
| 49 | ## Options
|
|---|
| 50 |
|
|---|
| 51 | ### `exportConditions`
|
|---|
| 52 |
|
|---|
| 53 | Type: `Array[...String]`<br>
|
|---|
| 54 | Default: `[]`
|
|---|
| 55 |
|
|---|
| 56 | Additional conditions of the package.json exports field to match when resolving modules. By default, this plugin looks for the `['default', 'module', 'import']` conditions when resolving imports.
|
|---|
| 57 |
|
|---|
| 58 | When using `@rollup/plugin-commonjs` v16 or higher, this plugin will use the `['default', 'module', 'require']` conditions when resolving require statements.
|
|---|
| 59 |
|
|---|
| 60 | Setting this option will add extra conditions on top of the default conditions. See https://nodejs.org/api/packages.html#packages_conditional_exports for more information.
|
|---|
| 61 |
|
|---|
| 62 | ### `browser`
|
|---|
| 63 |
|
|---|
| 64 | Type: `Boolean`<br>
|
|---|
| 65 | Default: `false`
|
|---|
| 66 |
|
|---|
| 67 | If `true`, instructs the plugin to use the `"browser"` property in `package.json` files to specify alternative files to load for bundling. This is useful when bundling for a browser environment. Alternatively, a value of `'browser'` can be added to the `mainFields` option. If `false`, any `"browser"` properties in package files will be ignored. This option takes precedence over `mainFields`.
|
|---|
| 68 |
|
|---|
| 69 | > This option does not work when a package is using [package entrypoints](https://nodejs.org/api/packages.html#packages_package_entry_points)
|
|---|
| 70 |
|
|---|
| 71 | ### `moduleDirectories`
|
|---|
| 72 |
|
|---|
| 73 | Type: `Array[...String]`<br>
|
|---|
| 74 | Default: `['node_modules']`
|
|---|
| 75 |
|
|---|
| 76 | One or more directories in which to recursively look for modules.
|
|---|
| 77 |
|
|---|
| 78 | ### `dedupe`
|
|---|
| 79 |
|
|---|
| 80 | Type: `Array[...String]`<br>
|
|---|
| 81 | Default: `[]`
|
|---|
| 82 |
|
|---|
| 83 | An `Array` of modules names, which instructs the plugin to force resolving for the specified modules to the root `node_modules`. Helps to prevent bundling the same package multiple times if package is imported from dependencies.
|
|---|
| 84 |
|
|---|
| 85 | ```js
|
|---|
| 86 | dedupe: ['my-package', '@namespace/my-package'];
|
|---|
| 87 | ```
|
|---|
| 88 |
|
|---|
| 89 | This will deduplicate bare imports such as:
|
|---|
| 90 |
|
|---|
| 91 | ```js
|
|---|
| 92 | import 'my-package';
|
|---|
| 93 | import '@namespace/my-package';
|
|---|
| 94 | ```
|
|---|
| 95 |
|
|---|
| 96 | And it will deduplicate deep imports such as:
|
|---|
| 97 |
|
|---|
| 98 | ```js
|
|---|
| 99 | import 'my-package/foo.js';
|
|---|
| 100 | import '@namespace/my-package/bar.js';
|
|---|
| 101 | ```
|
|---|
| 102 |
|
|---|
| 103 | ### `extensions`
|
|---|
| 104 |
|
|---|
| 105 | Type: `Array[...String]`<br>
|
|---|
| 106 | Default: `['.mjs', '.js', '.json', '.node']`
|
|---|
| 107 |
|
|---|
| 108 | Specifies the extensions of files that the plugin will operate on.
|
|---|
| 109 |
|
|---|
| 110 | ### `jail`
|
|---|
| 111 |
|
|---|
| 112 | Type: `String`<br>
|
|---|
| 113 | Default: `'/'`
|
|---|
| 114 |
|
|---|
| 115 | Locks the module search within specified path (e.g. chroot). Modules defined outside this path will be ignored by this plugin.
|
|---|
| 116 |
|
|---|
| 117 | ### `mainFields`
|
|---|
| 118 |
|
|---|
| 119 | Type: `Array[...String]`<br>
|
|---|
| 120 | Default: `['module', 'main']`<br>
|
|---|
| 121 | Valid values: `['browser', 'jsnext:main', 'module', 'main']`
|
|---|
| 122 |
|
|---|
| 123 | Specifies the properties to scan within a `package.json`, used to determine the bundle entry point. The order of property names is significant, as the first-found property is used as the resolved entry point. If the array contains `'browser'`, key/values specified in the `package.json` `browser` property will be used.
|
|---|
| 124 |
|
|---|
| 125 | ### `preferBuiltins`
|
|---|
| 126 |
|
|---|
| 127 | Type: `Boolean`<br>
|
|---|
| 128 | Default: `true` (with warnings if a builtin module is used over a local version. Set to `true` to disable warning.)
|
|---|
| 129 |
|
|---|
| 130 | If `true`, the plugin will prefer built-in modules (e.g. `fs`, `path`). If `false`, the plugin will look for locally installed modules of the same name.
|
|---|
| 131 |
|
|---|
| 132 | ### `modulesOnly`
|
|---|
| 133 |
|
|---|
| 134 | Type: `Boolean`<br>
|
|---|
| 135 | Default: `false`
|
|---|
| 136 |
|
|---|
| 137 | If `true`, inspect resolved files to assert that they are ES2015 modules.
|
|---|
| 138 |
|
|---|
| 139 | ### `resolveOnly`
|
|---|
| 140 |
|
|---|
| 141 | Type: `Array[...String|RegExp]`<br>
|
|---|
| 142 | Default: `null`
|
|---|
| 143 |
|
|---|
| 144 | An `Array` which instructs the plugin to limit module resolution to those whose names match patterns in the array. _Note: Modules not matching any patterns will be marked as external._
|
|---|
| 145 |
|
|---|
| 146 | Example: `resolveOnly: ['batman', /^@batcave\/.*$/]`
|
|---|
| 147 |
|
|---|
| 148 | ### `rootDir`
|
|---|
| 149 |
|
|---|
| 150 | Type: `String`<br>
|
|---|
| 151 | Default: `process.cwd()`
|
|---|
| 152 |
|
|---|
| 153 | Specifies the root directory from which to resolve modules. Typically used when resolving entry-point imports, and when resolving deduplicated modules. Useful when executing rollup in a package of a mono-repository.
|
|---|
| 154 |
|
|---|
| 155 | ```
|
|---|
| 156 | // Set the root directory to be the parent folder
|
|---|
| 157 | rootDir: path.join(process.cwd(), '..')
|
|---|
| 158 | ```
|
|---|
| 159 |
|
|---|
| 160 | ## `ignoreSideEffectsForRoot`
|
|---|
| 161 |
|
|---|
| 162 | If you use the `sideEffects` property in the package.json, by default this is respected for files in the root package. Set to `true` to ignore the `sideEffects` configuration for the root package.
|
|---|
| 163 |
|
|---|
| 164 | ## Preserving symlinks
|
|---|
| 165 |
|
|---|
| 166 | This plugin honours the rollup [`preserveSymlinks`](https://rollupjs.org/guide/en/#preservesymlinks) option.
|
|---|
| 167 |
|
|---|
| 168 | ## Using with @rollup/plugin-commonjs
|
|---|
| 169 |
|
|---|
| 170 | Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs):
|
|---|
| 171 |
|
|---|
| 172 | ```js
|
|---|
| 173 | // rollup.config.js
|
|---|
| 174 | import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|---|
| 175 | import commonjs from '@rollup/plugin-commonjs';
|
|---|
| 176 |
|
|---|
| 177 | export default {
|
|---|
| 178 | input: 'main.js',
|
|---|
| 179 | output: {
|
|---|
| 180 | file: 'bundle.js',
|
|---|
| 181 | format: 'iife',
|
|---|
| 182 | name: 'MyModule'
|
|---|
| 183 | },
|
|---|
| 184 | plugins: [nodeResolve(), commonjs()]
|
|---|
| 185 | };
|
|---|
| 186 | ```
|
|---|
| 187 |
|
|---|
| 188 | ## Resolving Built-Ins (like `fs`)
|
|---|
| 189 |
|
|---|
| 190 | By default this plugin will prefer built-ins over local modules, marking them as external.
|
|---|
| 191 |
|
|---|
| 192 | See [`preferBuiltins`](#preferbuiltins).
|
|---|
| 193 |
|
|---|
| 194 | To provide stubbed versions of Node built-ins, use a plugin like [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills) and set `preferBuiltins` to `false`. e.g.
|
|---|
| 195 |
|
|---|
| 196 | ```js
|
|---|
| 197 | import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|---|
| 198 | import nodePolyfills from 'rollup-plugin-node-polyfills';
|
|---|
| 199 | export default ({
|
|---|
| 200 | input: ...,
|
|---|
| 201 | plugins: [
|
|---|
| 202 | nodePolyfills(),
|
|---|
| 203 | nodeResolve({ preferBuiltins: false })
|
|---|
| 204 | ],
|
|---|
| 205 | external: builtins,
|
|---|
| 206 | output: ...
|
|---|
| 207 | })
|
|---|
| 208 | ```
|
|---|
| 209 |
|
|---|
| 210 | ## Resolving require statements
|
|---|
| 211 |
|
|---|
| 212 | According to [NodeJS module resolution](https://nodejs.org/api/packages.html#packages_package_entry_points) `require` statements should resolve using the `require` condition in the package exports field, while es modules should use the `import` condition.
|
|---|
| 213 |
|
|---|
| 214 | The node resolve plugin uses `import` by default, you can opt into using the `require` semantics by passing an extra option to the resolve function:
|
|---|
| 215 |
|
|---|
| 216 | ```js
|
|---|
| 217 | this.resolve(importee, importer, {
|
|---|
| 218 | skipSelf: true,
|
|---|
| 219 | custom: { 'node-resolve': { isRequire: true } }
|
|---|
| 220 | });
|
|---|
| 221 | ```
|
|---|
| 222 |
|
|---|
| 223 | ## Meta
|
|---|
| 224 |
|
|---|
| 225 | [CONTRIBUTING](/.github/CONTRIBUTING.md)
|
|---|
| 226 |
|
|---|
| 227 | [LICENSE (MIT)](/LICENSE)
|
|---|