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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@raytonx/config

v0.2.0

Published

Configuration module for NestJS applications.

Readme

@raytonx/config

用于 NestJS 应用的配置模块。

English version: README.en.md

安装

pnpm add @raytonx/config
npm i @raytonx/config
yarn add @raytonx/config

快速开始

import { Module } from "@nestjs/common";
import { ConfigModule } from "@raytonx/config";

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: "auto",
      values: {
        appName: "api",
      },
    }),
  ],
})
export class AppModule {}

envFilePath: "auto" 时,模块会在当前工作目录按以下顺序尝试加载:

.env
.env.local
.env.${NODE_ENV}
.env.${NODE_ENV}.local

后加载的文件会覆盖先加载的文件。process.env 的优先级最高。

环境文件

加载单个文件:

ConfigModule.forRoot({
  envFilePath: ".env.development",
});

加载多个文件:

ConfigModule.forRoot({
  envFilePath: [".env", ".env.local"],
});

禁用 env 文件加载:

ConfigModule.forRoot({
  envFilePath: false,
});

变量展开

默认开启变量展开:

APP_HOST=localhost
APP_PORT=3000
APP_URL=http://${APP_HOST}:${APP_PORT}

需要时可关闭:

ConfigModule.forRoot({
  envFilePath: "auto",
  expandVariables: false,
});

ConfigService

import { Injectable } from "@nestjs/common";
import { ConfigService } from "@raytonx/config";

@Injectable()
export class AppService {
  constructor(private readonly config: ConfigService) {}

  get port(): string {
    return this.config.getOrThrow("PORT");
  }
}

覆盖(Overrides)

通过 values 在模块初始化时显式覆盖配置:

ConfigModule.forRoot({
  envFilePath: "auto",
  values: {
    APP_NAME: "api",
  },
});

默认优先级如下:

env 文件 < values < process.env

Schema 校验

使用 Zod 在模块初始化阶段对配置进行校验与转换:

import { z } from "zod";

const configSchema = z.object({
  NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
  PORT: z.coerce.number().default(3000),
  DATABASE_URL: z.string().url(),
});

type AppConfig = z.infer<typeof configSchema>;

ConfigModule.forRoot<AppConfig>({
  isGlobal: true,
  envFilePath: "auto",
  schema: configSchema,
});

校验失败时,ConfigModule 会抛出 ConfigValidationError,并包含失败路径与 Zod 报错信息。

通过 ConfigService 可以访问校验后的值:

@Injectable()
export class AppService {
  constructor(private readonly config: ConfigService<AppConfig>) {}

  get port(): number {
    return this.config.getOrThrow("PORT");
  }
}

总结

  • 通过 envFilePath 加载 env 文件("auto" | string | string[] | false
  • "auto" 加载顺序:.env.env.local.env.${NODE_ENV}.env.${NODE_ENV}.local
  • 默认展开 ${VAR}$VARexpandVariables: false 可关闭)
  • 默认优先级:env 文件 < values < process.env
  • 通过 schema(Zod)进行校验与转换

完整示例

.env.development

NODE_ENV=development
PORT=3000
DATABASE_URL=https://example.invalid

src/app.module.ts

import { Module } from "@nestjs/common";
import { ConfigModule } from "@raytonx/config";
import { z } from "zod";

const configSchema = z.object({
  NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
  PORT: z.coerce.number().default(3000),
  DATABASE_URL: z.string().url(),
});

export type AppConfig = z.infer<typeof configSchema>;

@Module({
  imports: [
    ConfigModule.forRoot<AppConfig>({
      isGlobal: true,
      envFilePath: "auto",
      schema: configSchema,
    }),
  ],
})
export class AppModule {}

src/app.service.ts

import { Injectable } from "@nestjs/common";
import { ConfigService } from "@raytonx/config";

import type { AppConfig } from "./app.module";

@Injectable()
export class AppService {
  constructor(private readonly config: ConfigService<AppConfig>) {}

  get port(): number {
    return this.config.getOrThrow("PORT");
  }
}

src/main.ts

import { NestFactory } from "@nestjs/core";

import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}

void bootstrap();