A zero-dependency alternative to cosmiconfig for loading configuration files
89
Build a simple configuration extraction tool that searches for and extracts project configurations stored in package.json files.
Many Node.js tools store their configuration directly within the project's package.json file under specific properties. Your tool should be able to find package.json files in the directory tree and extract configurations from them.
Create a module that exports two functions:
extractConfig(configName, propertyPath)Searches upward from the current directory to find a package.json file that contains a configuration at the specified property path.
Parameters:
configName (string): The name to use when searching for configuration filespropertyPath (string or array, optional): The property path within package.json to extract from. If not provided, defaults to using configName as the top-level property. Can be a simple string like "prettier", a nested path like "config.myTool" (string with dots), or an array like ["config", "myTool"]Returns:
config and filepath properties if foundnull if no matching configuration is foundBehavior:
loadConfigFromFile(filepath, propertyPath)Loads a specific package.json file and extracts a configuration from it.
Parameters:
filepath (string): Path to the package.json file to loadpropertyPath (string or array): The property path to extract (e.g., "prettier", "config.eslint", or ["config", "eslint"])Returns:
config and filepath propertiesfilepath should be the absolute path to the file// Example 1: Simple top-level property
const result = await extractConfig('prettier');
// Returns: { config: {...}, filepath: '/path/to/package.json' }
// Example 2: Nested property using dot notation
const result = await extractConfig('eslint', 'config.eslint');
// Example 3: Nested property using array notation
const result = await extractConfig('myTool', ['config', 'tools', 'myTool']);
// Example 4: Load from specific file
const result = await loadConfigFromFile('./package.json', 'prettier');Provides configuration file search and loading capabilities.
config-extractor.js - Main implementationconfig-extractor.test.js - Test fileSetup: Create a package.json with:
{
"name": "test-project",
"prettier": {
"semi": false,
"singleQuote": true
}
}Test:
const result = await extractConfig('prettier');
assert(result !== null);
assert.deepEqual(result.config, { semi: false, singleQuote: true });
assert(result.filepath.endsWith('package.json'));Setup: Create a package.json with:
{
"name": "test-project",
"config": {
"myapp": {
"port": 3000,
"host": "localhost"
}
}
}Test:
const result = await extractConfig('myapp', 'config.myapp');
assert(result !== null);
assert.deepEqual(result.config, { port: 3000, host: 'localhost' });Setup: Use a package.json without the target property.
Test:
const result = await extractConfig('nonexistent');
assert.strictEqual(result, null);Setup: Create a package.json at a known path with a "prettier" property.
Test:
const result = await loadConfigFromFile('./package.json', 'prettier');
assert(result !== null);
assert(result.config !== undefined);
assert(result.filepath.endsWith('package.json'));Install with Tessl CLI
npx tessl i tessl/npm-lilconfigdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10