UNPKG

9.53 kBMarkdownView Raw
1# Netlify Config
2
3This library loads, validates, and normalizes the Netlify configuration.
4
5Netlify can be configured:
6
7- In the [build settings](https://docs.netlify.com/configure-builds/get-started/).
8- In a [`netlify.toml`](https://docs.netlify.com/configure-builds/file-based-configuration/) file in the repository root
9 directory or site `base` directory.
10
11# Install
12
13```bash
14npm install @netlify/config
15```
16
17# Usage (Node.js)
18
19## getNetlifyConfig(options?)
20
21`options`: `object?`\
22_Return value_: `Promise<object>`
23
24```js
25const getNetlifyConfig = require('@netlify/config')
26
27const exampleFunction = async function () {
28 const { config, configPath, buildDir, context, branch, token, siteInfo } = await getNetlifyConfig(options)
29 // {
30 // "siteInfo": {
31 // "id": "418b94bc-93cd-411a-937a-ae4c734f17c4",
32 // "name": "mick",
33 // "build_settings": {
34 // "cmd": "",
35 // "dir": "",
36 // "env": { ... },
37 // "functions_dir": "",
38 // "base": "",
39 // },
40 // ...
41 // },
42 // "accounts": [
43 // {
44 // "name": "my team",
45 // "slug": "me",
46 // ...
47 // },
48 // ...
49 // ],
50 // "addons": [],
51 // "env": {
52 // "NODE_VERSION": { "sources": ["configFile"], "value": "16" },
53 // ...
54 // },
55 // "configPath": "/home/me/code/cv-website/netlify.toml",
56 // "buildDir": "/home/me/code/cv-website",
57 // "repositoryRoot": "/home/me/code/cv-website",
58 // "config": {
59 // "functionsDirectory": "/home/me/code/cv-website/netlify/functions",
60 // "functionsDirectoryOrigin": "default",
61 // "functions": { "*": { "node_bundler": "esbuild" } },
62 // "plugins": [
63 // {
64 // "package": "@netlify/plugin-sitemap",
65 // "inputs": {},
66 // "origin": "config"
67 // }
68 // ],
69 // "build": {
70 // "publish": "/home/me/code/cv-website/build",
71 // "publishOrigin": "default",
72 // "command": "gulp build",
73 // "commandOrigin": "config",
74 // "functions": "/home/me/code/cv-website/netlify/functions"
75 // }
76 // },
77 // "context": "production",
78 // "branch": "main",
79 // "token": "564194bc-12cd-511a-037a-be4c734f17c4"
80 // }
81}
82```
83
84### Options
85
86The `options` are an optional `object` with the following properties.
87
88Those `options` are automatically set when using `@netlify/config` in the Netlify production CI or with Netlify CLI.
89
90#### debug
91
92_Type_: `boolean`\
93_Default value_: `false` unless the `NETLIFY_BUILD_DEBUG` environment variable is set.
94
95Prints debugging information showing the configuration being resolved.
96
97#### offline
98
99_Type_: `boolean`\
100_Default value_: `false`
101
102Do not send requests to the Netlify API to retrieve site settings.
103
104#### buffer
105
106_Type_: `boolean`\
107_Default value_: `false`
108
109When using [`debug`](#debug), returns the `logs` instead of printing them on the console.
110
111#### config
112
113_Type_: `string`
114
115Path to the `netlify.toml`. It is either an absolute path or a path relative to the [`cwd`](#cwd).
116
117If not specified, it is searched in the following directories (by highest priority order):
118
119- `base` directory
120- [`repositoryRoot`](#repositoryRoot)
121- current directory
122- any parent directory
123
124Otherwise, no `netlify.toml` is used.
125
126#### repositoryRoot
127
128_Type_: `string`\
129_Default value_: see [`cwd`](#cwd)
130
131Repository root directory. This is used in the following cases:
132
133- Searching for the `netlify.toml` (see [`config`](#config))
134- When a `base` directory was specified, its path is relative to the repository root directory
135- The `functions`, `edge_handlers` and `publish` directories are relative to the repository root directory or (if
136 specified) the `base` directory
137- Determining the [build directory](#builddir)
138
139If not specified, it is automatically guessed by looking for any `.git` directory from the [`cwd`](#cwd), and up. If
140none is found, the [`cwd`](#cwd) is used instead.
141
142#### cwd
143
144_Type_: `string`\
145_Default value_: `process.cwd()`
146
147Current directory. This is used in the following cases:
148
149- Searching for the `netlify.toml` (see [`config`](#config))
150- Searching for the [`repositoryRoot`](#repositoryroot)
151- In a monorepo, when stepping inside a specific package in the console, that package is automatically used as `base`
152 directory
153
154#### context
155
156_Type_: `string`\
157_Default value_: environment variable `CONTEXT`, or `"production"`
158
159[Deploy context](https://docs.netlify.com/site-deploys/overview/#deploy-contexts).
160
161The `netlify.toml` can contain `contexts.{CONTEXT}` properties, which are like `build` properties but only applied when
162`{CONTEXT}` matches.
163
164#### branch
165
166_Type_: `string`\
167_Default value_: environment variable `BRANCH`, current `git` branch, `"main"` or `"master"`.
168
169Same as [`context`](#context) but using a `git` branch name.
170
171#### token
172
173_Type_: `string`\
174_Default value_: environment variable `NETLIFY_AUTH_TOKEN`
175
176Netlify API token.
177
178This is used to retrieve [`siteInfo`](#siteinfo).
179
180#### host
181
182_Type_: `string`\
183_Default value_: `api.netlify.com`
184
185Host of the Netlify API.
186
187#### scheme
188
189_Type_: `string`\
190_Default value_: `https`
191
192Scheme/protocol of the Netlify API.
193
194#### pathPrefix
195
196_Type_: `string`\
197_Default value_: `/api/v1`
198
199Base path prefix of the Netlify API.
200
201#### siteId
202
203_Type_: `string`\
204_Default value_: environment variable `NETLIFY_SITE_ID`
205
206Netlify Site ID.
207
208This is used to retrieve [`siteInfo`](#siteinfo), [`accounts`](#accounts) and [`addons`](#addons).
209
210#### env
211
212_Type_: `object`
213
214Environment variable to use, in addition to the current `process.env`. This is used as the default values of other
215options.
216
217#### mode
218
219_Type_: `string`\
220_Default value_: `"require"`
221
222What is calling `@netlify/config`. Can be:
223
224- `"buildbot"`: Netlify production CI
225- `"cli"`: Netlify CLI
226- `"require"`: anything else
227
228This is used for the following cases:
229
230- if `mode` is `buildbot`, [`siteInfo`](#siteinfo), [`accounts`](#accounts) and [`addons`](#addons) are not retrieved
231 because they are also passed using another internal option.
232
233#### defaultConfig
234
235_Type_: `string`
236
237Configuration object used as default. This is an object serialized with JSON.
238
239#### inlineConfig
240
241_Type_: `object`
242
243Configuration object overriding any properties. This is a JavaScript object.
244
245#### configMutations
246
247_Type_: `array`
248
249Array of changes to apply to the configuration. Each change must be an object with three properties:
250
251- `keys`: array of keys targetting the property to change
252- `value`: new value of that property
253- `event`: build event when this change was applied, e.g. `onPreBuild`
254
255### Return value
256
257The return value is a `Promise` resolving to an `object` with the following properties.
258
259#### config
260
261_Type_: `object`
262
263Resolved configuration object.
264
265#### configPath
266
267_Type_: `string?`
268
269Absolute path to the `netlify.toml`, if this file exists.
270
271#### headersPath
272
273_Type_: `string`
274
275Absolute path to the `_headers`, even if this file does not exist.
276
277#### redirectsPath
278
279_Type_: `string`
280
281Absolute path to the `_redirects`, even if this file does not exist.
282
283#### buildDir
284
285_Type_: `string`
286
287Absolute path to the build directory.
288
289The build directory is the current directory in which most build operations, including the build command, execute. It is
290usually either the [`repositoryRoot`](#repositoryRoot) or (if specified) the `base` directory.
291
292#### repositoryRoot
293
294_Type_: `string`
295
296The computed value of [`repositoryRoot`](#repositoryRoot).
297
298#### context
299
300_Type_: `string`
301
302Resolved context. See the [`context`](#context) option.
303
304#### branch
305
306_Type_: `string`
307
308Resolved git branch. See the [`branch`](#branch) option.
309
310#### siteInfo
311
312_Type_: `object`
313
314Netlify Site information retrieved using the `getSite` Netlify API endpoint. This is used to retrieve Build settings set
315in the Netlify App: plugins, Build command, Publish directory, Functions directory, Base directory, Environment
316variables.
317
318This might be empty depending on the options passed.
319
320#### accounts
321
322_Type_: `object[]`
323
324Netlify accounts retrieved using the `listAccountsForUser` Netlify API endpoint. This is used to retrieve account-level
325environment variables.
326
327This might be empty depending on the options passed.
328
329#### addons
330
331_Type_: `object[]`
332
333Netlify addons retrieved using the `listServiceInstancesForSite` Netlify API endpoint. This is used to retrieve
334addon-specific environment variables.
335
336This might be empty depending on the options passed.
337
338#### token
339
340_Type_: `string`
341
342Netlify API token. This takes into account the [`token`](#token) option but also some Netlify-specific environment
343variables.
344
345#### api
346
347_Type_: `NetlifyClient?`
348
349Netlify [JavaScript client instance](https://github.com/netlify/js-client) used to retrieve [`siteInfo`](#siteinfo),
350[`accounts`](#accounts) and [`addons`](#addons).
351
352#### logs
353
354_Type_: `object?`
355
356When the [`buffer`](#buffer) option is used, this contains two arrays `stdout` and `stderr` with the logs.
357
358#### env
359
360_Type_: `object?`
361
362Site's environment variables. Each environment variable value is an object with the following properties:
363
364- `value` `string`
365- `sources` `string[]` among:
366 - `general`: general environment variables set for all sites
367 - `account`: environment variables set in the Netlify UI for a specific account
368 - `addons`: addon-specific environment variables
369 - `ui`: environment variables set in the Netlify UI for a specific site
370 - `configFile`: environment variables set in `netlify.toml`
371
372# Usage (CLI)
373
374```bash
375$ netlify-config
376```
377
378Like [`getNetlifyConfig()`](getnetlifyconfigoptions), but in the CLI. The return value is printed on `stdout`.
379
380The CLI flags use the same options.