Gathering detailed insights and metrics for @humanwhocodes/config-array
Gathering detailed insights and metrics for @humanwhocodes/config-array
Gathering detailed insights and metrics for @humanwhocodes/config-array
Gathering detailed insights and metrics for @humanwhocodes/config-array
DEPRECATED. Use eslint/config-array instead
npm install @humanwhocodes/config-array
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
31 Stars
176 Commits
12 Forks
4 Watchers
13 Branches
6 Contributors
Updated on Jul 09, 2025
Latest Version
0.13.0
Package Id
@humanwhocodes/config-array@0.13.0
Unpacked Size
57.01 kB
Size
15.52 kB
File Count
4
NPM Version
10.5.0
Node Version
20.12.2
Published on
Apr 17, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
If you find this useful, please consider supporting my work with a donation.
A config array is a way of managing configurations that are based on glob pattern matching of filenames. Each config array contains the information needed to determine the correct configuration for any file based on the filename.
In 2019, I submitted an ESLint RFC proposing a new way of configuring ESLint. The goal was to streamline what had become an increasingly complicated configuration process. Over several iterations, this proposal was eventually born.
The basic idea is that all configuration, including overrides, can be represented by a single array where each item in the array is a config object. Config objects appearing later in the array override config objects appearing earlier in the array. You can calculate a config for a given file by traversing all config objects in the array to find the ones that match the filename. Matching is done by specifying glob patterns in files
and ignores
properties on each config object. Here's an example:
1export default [ 2 3 // match all JSON files 4 { 5 name: "JSON Handler", 6 files: ["**/*.json"], 7 handler: jsonHandler 8 }, 9 10 // match only package.json 11 { 12 name: "package.json Handler", 13 files: ["package.json"], 14 handler: packageJsonHandler 15 } 16];
In this example, there are two config objects: the first matches all JSON files in all directories and the second matches just package.json
in the base path directory (all the globs are evaluated as relative to a base path that can be specified). When you retrieve a configuration for foo.json
, only the first config object matches so handler
is equal to jsonHandler
; when you retrieve a configuration for package.json
, handler
is equal to packageJsonHandler
(because both config objects match, the second one wins).
You can install the package using npm or Yarn:
1npm install @humanwhocodes/config-array --save 2 3# or 4 5yarn add @humanwhocodes/config-array
First, import the ConfigArray
constructor:
1import { ConfigArray } from "@humanwhocodes/config-array"; 2 3// or using CommonJS 4 5const { ConfigArray } = require("@humanwhocodes/config-array");
When you create a new instance of ConfigArray
, you must pass in two arguments: an array of configs and an options object. The array of configs is most likely read in from a configuration file, so here's a typical example:
1const configFilename = path.resolve(process.cwd(), "my.config.js");
2const { default: rawConfigs } = await import(configFilename);
3const configs = new ConfigArray(rawConfigs, {
4
5 // the path to match filenames from
6 basePath: process.cwd(),
7
8 // additional items in each config
9 schema: mySchema
10});
This example reads in an object or array from my.config.js
and passes it into the ConfigArray
constructor as the first argument. The second argument is an object specifying the basePath
(the directory in which my.config.js
is found) and a schema
to define the additional properties of a config object beyond files
, ignores
, and name
.
The schema
option is required for you to use additional properties in config objects. The schema is an object that follows the format of an ObjectSchema
. The schema specifies both validation and merge rules that the ConfigArray
instance needs to combine configs when there are multiple matches. Here's an example:
1const configFilename = path.resolve(process.cwd(), "my.config.js"); 2const { default: rawConfigs } = await import(configFilename); 3 4const mySchema = { 5 6 // define the handler key in configs 7 handler: { 8 required: true, 9 merge(a, b) { 10 if (!b) return a; 11 if (!a) return b; 12 }, 13 validate(value) { 14 if (typeof value !== "function") { 15 throw new TypeError("Function expected."); 16 } 17 } 18 } 19}; 20 21const configs = new ConfigArray(rawConfigs, { 22 23 // the path to match filenames from 24 basePath: process.cwd(), 25 26 // additional item schemas in each config 27 schema: mySchema, 28 29 // additional config types supported (default: []) 30 extraConfigTypes: ["array", "function"]; 31});
Config arrays can be multidimensional, so it's possible for a config array to contain another config array when extraConfigTypes
contains "array"
, such as:
1export default [ 2 3 // JS config 4 { 5 files: ["**/*.js"], 6 handler: jsHandler 7 }, 8 9 // JSON configs 10 [ 11 12 // match all JSON files 13 { 14 name: "JSON Handler", 15 files: ["**/*.json"], 16 handler: jsonHandler 17 }, 18 19 // match only package.json 20 { 21 name: "package.json Handler", 22 files: ["package.json"], 23 handler: packageJsonHandler 24 } 25 ], 26 27 // filename must match function 28 { 29 files: [ filePath => filePath.endsWith(".md") ], 30 handler: markdownHandler 31 }, 32 33 // filename must match all patterns in subarray 34 { 35 files: [ ["*.test.*", "*.js"] ], 36 handler: jsTestHandler 37 }, 38 39 // filename must not match patterns beginning with ! 40 { 41 name: "Non-JS files", 42 files: ["!*.js"], 43 settings: { 44 js: false 45 } 46 } 47];
In this example, the array contains both config objects and a config array. When a config array is normalized (see details below), it is flattened so only config objects remain. However, the order of evaluation remains the same.
If the files
array contains a function, then that function is called with the absolute path of the file and is expected to return true
if there is a match and false
if not. (The ignores
array can also contain functions.)
If the files
array contains an item that is an array of strings and functions, then all patterns must match in order for the config to match. In the preceding examples, both *.test.*
and *.js
must match in order for the config object to be used.
If a pattern in the files array begins with !
then it excludes that pattern. In the preceding example, any filename that doesn't end with .js
will automatically get a settings.js
property set to false
.
You can also specify an ignores
key that will force files matching those patterns to not be included. If the ignores
key is in a config object without any other keys, then those ignores will always be applied; otherwise those ignores act as exclusions. Here's an example:
1export default [ 2 3 // Always ignored 4 { 5 ignores: ["**/.git/**", "**/node_modules/**"] 6 }, 7 8 // .eslintrc.js file is ignored only when .js file matches 9 { 10 files: ["**/*.js"], 11 ignores: [".eslintrc.js"] 12 handler: jsHandler 13 } 14];
You can use negated patterns in ignores
to exclude a file that was already ignored, such as:
1export default [ 2 3 // Ignore all JSON files except tsconfig.json 4 { 5 files: ["**/*"], 6 ignores: ["**/*.json", "!tsconfig.json"] 7 }, 8 9];
Config arrays can also include config functions when extraConfigTypes
contains "function"
. A config function accepts a single parameter, context
(defined by you), and must return either a config object or a config array (it cannot return another function). Config functions allow end users to execute code in the creation of appropriate config objects. Here's an example:
1export default [ 2 3 // JS config 4 { 5 files: ["**/*.js"], 6 handler: jsHandler 7 }, 8 9 // JSON configs 10 function (context) { 11 return [ 12 13 // match all JSON files 14 { 15 name: context.name + " JSON Handler", 16 files: ["**/*.json"], 17 handler: jsonHandler 18 }, 19 20 // match only package.json 21 { 22 name: context.name + " package.json Handler", 23 files: ["package.json"], 24 handler: packageJsonHandler 25 } 26 ]; 27 } 28];
When a config array is normalized, each function is executed and replaced in the config array with the return value.
Note: Config functions can also be async.
Once a config array has been created and loaded with all of the raw config data, it must be normalized before it can be used. The normalization process goes through and flattens the config array as well as executing all config functions to get their final values.
To normalize a config array, call the normalize()
method and pass in a context object:
1await configs.normalize({ 2 name: "MyApp" 3});
The normalize()
method returns a promise, so be sure to use the await
operator. The config array instance is normalized in-place, so you don't need to create a new variable.
If you want to disallow async config functions, you can call normalizeSync()
instead. This method is completely synchronous and does not require using the await
operator as it does not return a promise:
1await configs.normalizeSync({ 2 name: "MyApp" 3});
Important: Once a ConfigArray
is normalized, it cannot be changed further. You can, however, create a new ConfigArray
and pass in the normalized instance to create an unnormalized copy.
To get the config for a file, use the getConfig()
method on a normalized config array and pass in the filename to get a config for:
1// pass in absolute filename
2const fileConfig = configs.getConfig(path.resolve(process.cwd(), "package.json"));
The config array always returns an object, even if there are no configs matching the given filename. You can then inspect the returned config object to determine how to proceed.
A few things to keep in mind:
files
, ignores
, or name
properties; the only properties on the object will be the other configuration options specified.getConfig()
with the same filename will return in a fast lookup rather than another calculation.files
key. A config will not be generated without matching a files
key (configs without a files
key are only applied when another config with a files
key is applied; configs without files
are never applied on their own). Any config with a files
key entry ending with /**
or /*
will only be applied if another entry in the same files
key matches or another config matches.You can determine if a file is ignored by using the isFileIgnored()
method and passing in the absolute path of any file, as in this example:
1const ignored = configs.isFileIgnored('/foo/bar/baz.txt');
A file is considered ignored if any of the following is true:
foo
is in ignores
, then foo/a.js
is considered ignored.foo
is in ignores
, then foo/baz/a.js
is considered ignored.**/a.js
is in ignores
, then foo/a.js
and foo/baz/a.js
are considered ignored.files
and also in ignores
. For example, if **/*.js
is in files
and **/a.js
is in ignores
, then foo/a.js
and foo/baz/a.js
are considered ignored.basePath
. If the basePath
is /usr/me
, then /foo/a.js
is considered ignored.For directories, use the isDirectoryIgnored()
method and pass in the absolute path of any directory, as in this example:
1const ignored = configs.isDirectoryIgnored('/foo/bar/');
A directory is considered ignored if any of the following is true:
foo
is in ignores
, then foo/baz
is considered ignored.foo
is in ignores
, then foo/bar/baz/a.js
is considered ignored.**/a.js
is in ignores
, then foo/a.js
and foo/baz/a.js
are considered ignored.files
and also in ignores
. For example, if **/*.js
is in files
and **/a.js
is in ignores
, then foo/a.js
and foo/baz/a.js
are considered ignored.basePath
. If the basePath
is /usr/me
, then /foo/a.js
is considered ignored.Important: A pattern such as foo/**
means that foo
and foo/
are not ignored whereas foo/bar
is ignored. If you want to ignore foo
and all of its subdirectories, use the pattern foo
or foo/
in ignores
.
Each ConfigArray
aggressively caches configuration objects to avoid unnecessary work. This caching occurs in two ways:
getConfig()
whenever you pass the same filename in.1,5,7
. That way, if another file is passed that matches the same config elements, the result is already known and doesn't have to be recalculated. That means two files that match all the same elements will return the same config from getConfig()
.The design of this project was influenced by feedback on the ESLint RFC, and incorporates ideas from:
Apache 2.0
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
Found 7/15 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
10 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More