JSPM

  • Created
  • Published
  • Downloads 160502
  • Score
    100M100P100Q151174F
  • License MIT

Load a esm or cjs config from the file system

Package Exports

  • @web/config-loader

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@web/config-loader) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Config Loader

Load user config files for node js projects. Supports loading config as es module or common js module, based on the user's node version, package type and file extension. Prints helpful error messages when invalid syntax combinations are used.

Follows node's logic for deciding how to load a file. .mjs files are loaded as es module, .cjs as common js. .js files are loaded based on the type field of the package.json.

Usage

npm i -D @web/config-loader

CommonJS:

const { readConfig, ConfigLoaderError } = require('@web/config-loader');

(async () => {
  try {
    // will look for:
    // process.cwd() + 'my-project.config.mjs'
    // process.cwd() + 'my-project.config.cjs'
    // process.cwd() + 'my-project.config.js'
    const config = await readConfig('my-project.config');
  } catch (error) {
    if (error instanceof ConfigLoaderError) {
      // If the error is a ConfigLoaderError it has a human readable error message
      // there is no need to print the stack trace.
      console.error(error.message);
      return;
    }
    console.error(error);
    return;
  }
})();

Es module:

import ConfigLoader from '@web/config-loader';
const { readConfig, ConfigLoaderError } = ConfigLoader;

// samve as above

Custom config file

If you want to let users define a custom config file location, you can pass this as a second optional parameter.

const { readConfig, ConfigLoaderError } = require('@web/config-loader');

(async () => {
  try {
    const optionalCustomConfigFilePath = '...';
    const config = await readConfig('my-project.config', optionalCustomConfigFilePath);
  } catch (error) {
    if (error instanceof ConfigLoaderError) {
      // If the error is a ConfigLoaderError it has a human readable error message
      // there is no need to print the stack trace.
      console.error(error.message);
      return;
    }
    console.error(error);
    return;
  }
})();