github.com/StackExchange/dnscontrol/v4@v4.11.0/documentation/language-reference/top-level-functions/require.md (about) 1 --- 2 name: require 3 parameters: 4 - path 5 ts_ignore: true 6 --- 7 8 `require(...)` loads the specified JavaScript or JSON file, allowing 9 to split your configuration across multiple files. 10 11 A better name for this function might be "include". 12 13 If the supplied `path` string ends with `.js`, the file is interpreted 14 as JavaScript code, almost as though its contents had been included in 15 the currently-executing file. If the path string ends with `.json`, 16 `require()` returns the `JSON.parse()` of the file's contents. 17 18 If the path string begins with a `./`, it is interpreted relative to 19 the currently-loading file (which may not be the file where the 20 `require()` statement is, if called within a function). Otherwise it 21 is interpreted relative to the program's working directory at the time 22 of the call. 23 24 ### Example 1: Simple 25 26 In this example, we separate our macros in one file, and put groups of domains 27 in 3 other files. The result is a cleaner separation of code vs. domains. 28 29 {% code title="dnsconfig.js" %} 30 ```javascript 31 require("lib/macros.json"); 32 33 require("domains/main.json"); 34 require("domains/parked.json"); 35 require("domains/otherstuff.json"); 36 ``` 37 {% endcode %} 38 39 ### Example 2: Complex 40 41 Here's a more complex example: 42 43 {% code title="dnsconfig.js" %} 44 ```javascript 45 require("kubernetes/clusters.js"); 46 47 D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER), 48 IncludeKubernetes(), 49 END); 50 ``` 51 {% endcode %} 52 53 {% code title="kubernetes/clusters.js" %} 54 ```javascript 55 require("./clusters/prod.js"); 56 require("./clusters/dev.js"); 57 58 function IncludeKubernetes() { 59 return [includeK8Sprod(), includeK8Sdev()]; 60 } 61 ``` 62 {% endcode %} 63 64 {% code title="kubernetes/clusters/prod.js" %} 65 ```javascript 66 function includeK8Sprod() { 67 return [ 68 // ... 69 ]; 70 } 71 ``` 72 {% endcode %} 73 74 {% code title="kubernetes/clusters/dev.js" %} 75 ```javascript 76 function includeK8Sdev() { 77 return [ 78 // ... 79 ]; 80 } 81 ``` 82 {% endcode %} 83 84 ### Example 3: JSON 85 86 Requiring JSON files initializes variables: 87 88 {% code title="dnsconfig.js" %} 89 ```javascript 90 var domains = require("./domain-ip-map.json") 91 92 for (var domain in domains) { 93 D(domain, REG_MY_PROVIDER, PROVIDER, 94 A("@", domains[domain]) 95 ); 96 } 97 ``` 98 {% endcode %} 99 100 {% code title="domain-ip-map.json" %} 101 ```javascript 102 { 103 "example.com": "1.1.1.1", 104 "other-example.com``": "5.5.5.5" 105 } 106 ``` 107 {% endcode %} 108 109 # Notes 110 111 `require()` is *much* closer to PHP's `include()` function than it 112 is to node's `require()`. 113 114 Node's `require()` only includes a file once. 115 In contrast, DNSControl's `require()` is actually an imperative command to 116 load the file and execute the code or parse the data from it. For example if 117 two files both `require("./tools.js")`, then it will be 118 loaded twice, whereas in node.js it would only be loaded once.