UNPKG

8.27 kBMarkdownView Raw
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[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](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
16This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
17
18## Install
19
20Using npm:
21
22```console
23npm install @rollup/plugin-node-resolve --save-dev
24```
25
26## Usage
27
28Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
29
30```js
31import { nodeResolve } from '@rollup/plugin-node-resolve';
32
33export default {
34 input: 'src/index.js',
35 output: {
36 dir: 'output',
37 format: 'cjs'
38 },
39 plugins: [nodeResolve()]
40};
41```
42
43Then 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
47This 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
53Type: `Array[...String]`<br>
54Default: `[]`
55
56Additional 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
58When using `@rollup/plugin-commonjs` v16 or higher, this plugin will use the `['default', 'module', 'require']` conditions when resolving require statements.
59
60Setting 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
62In order to get the [resolution behavior of Node.js](https://nodejs.org/api/packages.html#packages_conditional_exports), set this to `['node']`.
63
64### `browser`
65
66Type: `Boolean`<br>
67Default: `false`
68
69If `true`, instructs the plugin to use the browser module resolutions in `package.json` and adds `'browser'` to `exportConditions` if it is not present so browser conditionals in `exports` are applied. If `false`, any browser properties in package files will be ignored. Alternatively, a value of `'browser'` can be added to both the `mainFields` and `exportConditions` options, however this option takes precedence over `mainFields`.
70
71> This option does not work when a package is using [package entrypoints](https://nodejs.org/api/packages.html#packages_package_entry_points)
72
73### `moduleDirectories`
74
75Type: `Array[...String]`<br>
76Default: `['node_modules']`
77
78One or more directories in which to recursively look for modules.
79
80### `dedupe`
81
82Type: `Array[...String]`<br>
83Default: `[]`
84
85An `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.
86
87```js
88dedupe: ['my-package', '@namespace/my-package'];
89```
90
91This will deduplicate bare imports such as:
92
93```js
94import 'my-package';
95import '@namespace/my-package';
96```
97
98And it will deduplicate deep imports such as:
99
100```js
101import 'my-package/foo.js';
102import '@namespace/my-package/bar.js';
103```
104
105### `extensions`
106
107Type: `Array[...String]`<br>
108Default: `['.mjs', '.js', '.json', '.node']`
109
110Specifies the extensions of files that the plugin will operate on.
111
112### `jail`
113
114Type: `String`<br>
115Default: `'/'`
116
117Locks the module search within specified path (e.g. chroot). Modules defined outside this path will be ignored by this plugin.
118
119### `mainFields`
120
121Type: `Array[...String]`<br>
122Default: `['module', 'main']`<br>
123Valid values: `['browser', 'jsnext:main', 'module', 'main']`
124
125Specifies 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.
126
127### `preferBuiltins`
128
129Type: `Boolean`<br>
130Default: `true` (with warnings if a builtin module is used over a local version. Set to `true` to disable warning.)
131
132If `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.
133
134### `modulesOnly`
135
136Type: `Boolean`<br>
137Default: `false`
138
139If `true`, inspect resolved files to assert that they are ES2015 modules.
140
141### `resolveOnly`
142
143Type: `Array[...String|RegExp] | (module: string) => boolean`<br>
144Default: `null`
145
146An `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._
147
148Alternatively, you may pass in a function that returns a boolean to confirm whether the module should be included or not.
149
150Examples:
151
152- `resolveOnly: ['batman', /^@batcave\/.*$/]`
153- `resolveOnly: module => !module.includes('joker')`
154
155### `rootDir`
156
157Type: `String`<br>
158Default: `process.cwd()`
159
160Specifies 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.
161
162```
163// Set the root directory to be the parent folder
164rootDir: path.join(process.cwd(), '..')
165```
166
167### `ignoreSideEffectsForRoot`
168
169If 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.
170
171## Preserving symlinks
172
173This plugin honours the rollup [`preserveSymlinks`](https://rollupjs.org/guide/en/#preservesymlinks) option.
174
175## Using with @rollup/plugin-commonjs
176
177Since 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):
178
179```js
180// rollup.config.js
181import { nodeResolve } from '@rollup/plugin-node-resolve';
182import commonjs from '@rollup/plugin-commonjs';
183
184export default {
185 input: 'main.js',
186 output: {
187 file: 'bundle.js',
188 format: 'iife',
189 name: 'MyModule'
190 },
191 plugins: [nodeResolve(), commonjs()]
192};
193```
194
195## Resolving Built-Ins (like `fs`)
196
197By default this plugin will prefer built-ins over local modules, marking them as external.
198
199See [`preferBuiltins`](#preferbuiltins).
200
201To 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.
202
203```js
204import { nodeResolve } from '@rollup/plugin-node-resolve';
205import nodePolyfills from 'rollup-plugin-node-polyfills';
206export default ({
207 input: ...,
208 plugins: [
209 nodePolyfills(),
210 nodeResolve({ preferBuiltins: false })
211 ],
212 external: builtins,
213 output: ...
214})
215```
216
217## Resolving require statements
218
219According 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.
220
221The node resolve plugin uses `import` by default, you can opt into using the `require` semantics by passing an extra option to the resolve function:
222
223```js
224this.resolve(importee, importer, {
225 skipSelf: true,
226 custom: { 'node-resolve': { isRequire: true } }
227});
228```
229
230## Meta
231
232[CONTRIBUTING](/.github/CONTRIBUTING.md)
233
234[LICENSE (MIT)](/LICENSE)