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 |
|
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 resolve 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: [resolve()]
|
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 | ## Options
|
46 |
|
47 | ### `mainFields`
|
48 |
|
49 | Type: `Array[String]`<br>
|
50 | Default: `['module', 'main']`
|
51 |
|
52 | The fields to scan in a package.json to determine the entry point if this list contains "browser", overrides specified in "pkg.browser" will be used
|
53 |
|
54 | ### `module`
|
55 |
|
56 | Type: `Boolean`<br>
|
57 | Default: `true`
|
58 |
|
59 | DEPRECATED: use "mainFields" instead
|
60 | Use "module" field for ES6 module if possible
|
61 |
|
62 | ### `jsnext`
|
63 |
|
64 | Type: `Boolean`<br>
|
65 | Default: `false`
|
66 |
|
67 | DEPRECATED: use "mainFields" instead
|
68 | Use "jsnext:main" if possible, legacy field pointing to ES6 module in third-party libraries, deprecated in favor of "pkg.module", see: https://github.com/rollup/rollup/wiki/pkg.module
|
69 |
|
70 | ### `main`
|
71 |
|
72 | Type: `Boolean`<br>
|
73 | Default: `true`
|
74 |
|
75 | DEPRECATED: use "mainFields" instead
|
76 | Use "main" field or index.js, even if it's not an ES6 module (needs to be converted from CommonJS to ES6) – see https://github.com/rollup/rollup-plugin-commonjs
|
77 |
|
78 | ### `browser`
|
79 |
|
80 | Type: `Boolean`<br>
|
81 | Default: `false`
|
82 |
|
83 | Some package.json files have a "browser" field which specifies alternative files to load for people bundling for the browser. If that's you, either use this option or add "browser" to the "mainFields" option, otherwise pkg.browser will be ignored
|
84 |
|
85 | ### `extensions`
|
86 |
|
87 | Type: `Array[String]`<br>
|
88 | Default: `['.mjs', '.js', '.json', '.node']`
|
89 |
|
90 | Resolve extensions other than .js in the order specified.
|
91 |
|
92 | ### `preferBuiltins`
|
93 |
|
94 | Type: `Boolean`<br>
|
95 | Default: `true`
|
96 |
|
97 | Whether to prefer built-in modules (e.g. `fs`, `path`) or local ones with the same names
|
98 |
|
99 | ### `jail`
|
100 |
|
101 | Type: `String`<br>
|
102 | Default: `'/'`
|
103 |
|
104 | Lock the module search in this path (like a chroot). Modules defined outside this path will be marked as external.
|
105 |
|
106 | ### `only`
|
107 |
|
108 | Type: `Array[String|RegExp]`<br>
|
109 | Default: `null`
|
110 |
|
111 | Example: `only: ['some_module', /^@some_scope\/.*$/]`
|
112 |
|
113 | ### `modulesOnly`
|
114 |
|
115 | Type: `Boolean`<br>
|
116 | Default: `false`
|
117 |
|
118 | If true, inspect resolved files to check that they are ES2015 modules.
|
119 |
|
120 | ### `dedupe`
|
121 |
|
122 | Type: `Array[String]`<br>
|
123 | Default: `[]`
|
124 |
|
125 | Force resolving for these modules to root's node_modules that helps to prevent bundling the same package multiple times if package is imported from dependencies.
|
126 |
|
127 | ```
|
128 | dedupe: [ 'react', 'react-dom' ]
|
129 | ```
|
130 |
|
131 | ### `customResolveOptions`
|
132 |
|
133 | Type: `Boolean`<br>
|
134 | Default: `null`
|
135 |
|
136 | Any additional options that should be passed through to node-resolve.
|
137 |
|
138 | ```
|
139 | customResolveOptions: {
|
140 | moduleDirectory: 'js_modules'
|
141 | }
|
142 | ```
|
143 |
|
144 | ## Using with rollup-plugin-commonjs
|
145 |
|
146 | 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/rollup-plugin-commonjs):
|
147 |
|
148 | ```js
|
149 | // rollup.config.js
|
150 | import resolve from '@rollup/plugin-node-resolve';
|
151 | import commonjs from 'rollup-plugin-commonjs';
|
152 |
|
153 | export default {
|
154 | input: 'main.js',
|
155 | output: {
|
156 | file: 'bundle.js',
|
157 | format: 'iife',
|
158 | name: 'MyModule'
|
159 | },
|
160 | plugins: [resolve(), commonjs()]
|
161 | };
|
162 | ```
|
163 |
|
164 | ## Resolving Built-Ins (like `fs`)
|
165 |
|
166 | This plugin won't resolve any builtins (e.g. `fs`). If you need to resolve builtins you can install local modules and set `preferBuiltins` to `false`, or install a plugin like [rollup-plugin-node-builtins](https://github.com/calvinmetcalf/rollup-plugin-node-builtins) which provides stubbed versions of these methods.
|
167 |
|
168 | If you want to silence warnings about builtins, you can add the list of builtins to the `externals` option; like so:
|
169 |
|
170 | ```js
|
171 | import resolve from '@rollup/plugin-node-resolve';
|
172 | import builtins from 'builtin-modules'
|
173 | export default ({
|
174 | input: ...,
|
175 | plugins: [resolve()],
|
176 | external: builtins,
|
177 | output: ...
|
178 | })
|
179 | ```
|
180 |
|
181 | ## Meta
|
182 |
|
183 | [CONTRIBUTING](/.github/CONTRIBUTING.md)
|
184 |
|
185 | [LICENSE (MIT)](/LICENSE)
|