npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@andyrmitchell/cli-config-file-ts

v0.0.7

Published

Load and validate TypeScript config files with optional Zod schema support. Lightweight, zero dependencies, comprehensive error handling.

Readme

@andyrmitchell/cli-config-file-ts

Load and validate TypeScript config files — modern, minimal, and dependency-free (unless you choose to validate with Zod).

✨ Features

  • Modern ESM only — small, fast, no CommonJS legacy.
  • Zero dependencies — unless you opt into schema validation with zod.
  • Supports native TS environments — works out of the box in runtimes like Bun or tsx.
  • Automatic fallback to transpilation — no TS loader? It’ll transpile your config on the fly.
  • Optional Zod validation — validate your config with a schema (via a separate export).
  • Create default config if missing — optionally auto-generate a config file.

🚀 Installation

npm install @andyrmitchell/cli-config-file-ts

If you want to use Zod validation, install zod:

npm install zod

⚡ Quick Usage

Load a config file

import { loadTsConfig } from "@andyrmitchell/cli-config-file-ts";

const config = await loadTsConfig("/absolute/path/to/config.ts");
console.log(config);

👉 You can also explicitly type it if needed:

const config = await loadTsConfig<{ port: number }>(
  "/absolute/path/to/config.ts"
);
// config: {port: number}

Note if you want to type it, it's best to use a zod schema to validate it (see below).


Provide a default if file doesn’t exist

const config = await loadTsConfig("/absolute/path/to/config.ts", {
  config: { port: 3000, host: "localhost" },
});

👉 If the file is missing:

  • It writes the default config.
  • Throws an error prompting you to check the new file to confirm you're happy with the defaults and then re-run it.

👉 If you want to immediately use the generated config:

const config = await loadTsConfig("/absolute/path/to/config.ts", {
  config: { port: 3000, host: "localhost" },
  immediatelyUseConfig: true,
});

Validate config with Zod (with type inference)

import { z } from "zod";
import { loadTsConfigWithSchema } from "@andyrmitchell/cli-config-file-ts/schema";

// Define your Zod schema
const schema = z.object({
  port: z.number(),
  host: z.string(),
});

// The returned config is fully typed!
const config = await loadTsConfigWithSchema(
  "/absolute/path/to/config.ts",
  schema
);

// `config` is inferred as { port: number; host: string }
console.log(config.port.toFixed(0)); // fully typed as number
console.log(config.host.toUpperCase()); // fully typed as string

If validation fails, you’ll get clear error messages like:

Config file did not match schema. Validation errors:
port: Expected number, received string

🌟 Example config.ts

export default {
  port: 3000,
  host: "localhost",
};

or

export const config = {
  port: 3000,
  host: "localhost",
};

📝 Notes

  • The path you pass must be absolute.
  • Requires Node.js >=19.0.0.
  • Supports native TypeScript loading in compatible environments (e.g. Bun, tsx). Falls back to internal transpilation otherwise.

🛡️ Error handling with isKnownCause

If you want to surface clear, typed information from loader errors you can check the Error.cause against the helper function isKnownCause.

try {
  const config = await loadTsConfig("/absolute/path/to/config.ts");
  // …use your config here…
} catch (err) {
  // First, make sure it's an `Error` and has a `.cause`
  if (err instanceof Error && isKnownCause(err.cause)) {
    switch (err.cause.type) {
      case "file_not_found":
        console.error("Config file not found:", err.cause.reason);
        break;
      case "invalid_config_format":
        console.error("Config failed schema validation.");
        break;
      default:
        console.error("Unhandled config loader error:", err.cause.type);
    }
  } else {
    // Unknown error — re‑throw or handle generically
    throw err;
  }
}

📦 Exports

| Export | What it does | | ------------------------------------------ | ------------------------------------------ | | @andyrmitchell/cli-config-file-ts | Core loader without schema validation | | @andyrmitchell/cli-config-file-ts/schema | Loader with optional Zod schema validation |