@dunx/core

中文文档
3.10.6发布于 2026/10/30MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 6237

DI container, modules, lifecycle and the injectable Logger contract for the dunx framework

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. What is here
  4. Subpaths
  5. Notes
  6. License

@dunx/core

The dependency injection container, modules, lifecycle, configuration, and the Logger and RequestContext contracts for dunx.

No dependencies. The other dunx packages can inject a Logger without installing a logging library.

Install

bun add @dunx/core @dunx/transform

Usage

Constructor injection needs no decorators:

import { AppFactory, Module } from '@dunx/core';

export class UsersService {
  constructor(private readonly repo: UsersRepository) {}
}

@Module({
  providers: [UsersRepository, UsersService],
  exports: [UsersService], // absent means nothing is exported
})
export class UsersModule {}

const app = await AppFactory.create(AppModule);

You do not need @Injectable, @Inject, reflect-metadata or experimentalDecorators. @dunx/transform reads each constructor's parameter types when the file loads. Turn it on in bunfig.toml, once for the app and once for tests:

preload = ["@dunx/transform/preload"]

[test]
preload = ["@dunx/transform/preload"]

What is here

AreaWhat it coversGuide
ProvidersConstructor injection, provide(), token(), inject()Providers
ModulesScoping, imports, exports, global, forRootModules
LifecycleonInit, onBeforeShutdown, onShutdown, signal handlersLifecycle
ConfigurationConfigModule.forRoot, a validate function or a schemaConfiguration
LoggingThe Logger contract and ConsoleLoggerLogging
EventsEventBus, AppEvent, @OnEvent, EventRegistryEvents
StatsDurations, Counter, Gauge, RuntimeStats, EventLoopLagMetrics
ResilienceResiliencePolicy, timeout, retry, backoff, jitter, fallback, CircuitBreaker, BulkheadResilience
TracingThe Tracer contract, NoopTracer, and OtelModule on /otelTracing

Subpaths

SubpathContains
@dunx/coreEverything above. Never imports @opentelemetry/api
@dunx/core/otelOtelModule and OtelTracer, on the optional peer @opentelemetry/api
bun add @opentelemetry/api   # only for @dunx/core/otel, ^1.4.0

@opentelemetry/api is a peer dependency so that your SDK and dunx use the same copy. With two copies installed, spans can go missing without an error. dunx does not include an SDK or an exporter: register your own before calling create.

Notes

  • Each module is its own scope. A module can use what it declares and what the modules it imports export. List what other modules may use in exports; leave exports out and nothing is shared. global: true makes a module's exports available everywhere.

  • If a constructor parameter's type does not exist at runtime (an interface, a primitive, a union or a type-only import), boot fails with an error naming that parameter. You never get a silent undefined. A parameter with a default value keeps its default.

  • Logger, RequestContext and Tracer can always be injected. The defaults are ConsoleLogger, AsyncRequestContext (on AsyncLocalStorage) and NoopTracer, and a module that binds one replaces the default. @dunx/http relies on this to log requests in an app with no logging module.

  • ConsoleLogger collects info and lower into one write per event-loop turn. warn and higher are written at once, along with anything waiting before them.

  • The stats classes use only node:perf_hooks and process, so the package still has no dependencies.

  • Durations fixes four problems with Node's native histogram:

    Native histogramDurations
    record(0) throws ERR_OUT_OF_RANGEA value under 1 is recorded as 1
    An empty histogram reports min 9223372036854776000, mean NaNAn empty snapshot is { count: 0 }
    percentiles is a Map of bigint, which serialises to {}percentile(n) returns a number
    mean costs 42.2 usNo mean

License

MIT

版本与发布记录

@dunx/core 共发布 83 个版本,首次发布于 2026/08/02;过去 12 个月发布了 83 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.10.6latest2026/10/3014 天前
3.10.52026/10/2222 天前
3.10.42026/10/2024 天前
3.10.32026/10/1430 天前
3.10.22026/10/081 个月前
3.10.12026/09/261 个月前

关于 @dunx/core 的常见问题

@dunx/core 是什么?

DI container, modules, lifecycle and the injectable Logger contract for the dunx framework

如何安装 @dunx/core?

在项目目录运行 npm install @dunx/core,或使用 pnpm add / yarn add / bun add。

@dunx/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dunx/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dunx/core 可以商用吗?

@dunx/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dunx/core 的下载量有多少?

根据 npm 官方下载统计,@dunx/core 在 2026/10/13 – 2026/11/11 这 30 天内被下载 6,237 次,日均约 208 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dunx/core 还在维护吗?

最新版本 3.10.6 发布于 2026/10/30(14 天前);至今共发布 83 个版本,其中过去 12 个月发布了 83 个;首次发布于 2026/08/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。