@eggjs/core

中文文档
7.1.0发布于 2026/10/07MITTypeScriptES Module构建来源已验证近 30 天下载 6312

A core plugin framework based on @eggjs/koa

原始使用文档
文档目录13 节
  1. Usage
  2. EggLoader
  3. constructor
  4. High Level APIs
  5. Low Level APIs
  6. LoaderOptions
  7. Timing
  8. start
  9. end
  10. toJSON
  11. Questions & Suggestions
  12. License
  13. Contributors

@eggjs/core

NPM version Known Vulnerabilities npm download Node.js Version

A core plugin framework based on @eggjs/koa. Support Commonjs and ESM both by tshy.

Don't use it directly, see egg.

Usage

Directory structure

├── package.json
├── app.ts (optional)
├── agent.ts (optional)
├── app
|   ├── router.ts
│   ├── controller
│   │   └── home.ts
|   ├── extend (optional)
│   |   ├── helper.ts (optional)
│   |   ├── filter.ts (optional)
│   |   ├── request.ts (optional)
│   |   ├── response.ts (optional)
│   |   ├── context.ts (optional)
│   |   ├── application.ts (optional)
│   |   └── agent.ts (optional)
│   ├── service (optional)
│   ├── middleware (optional)
│   │   └── response_time.ts
│   └── view (optional)
|       ├── layout.html
│       └── home.html
├── config
|   ├── config.default.ts
│   ├── config.prod.ts
|   ├── config.test.ts (optional)
|   ├── config.local.ts (optional)
|   ├── config.unittest.ts (optional)
│   └── plugin.ts

Then you can start with code below

import { EggCore as Application } from '@eggjs/core';

const app = new Application({
  baseDir: '/path/to/app',
});
app.ready(() => {
  app.listen(3000);
});

EggLoader

EggLoader can easily load files or directories in your egg project. In addition, you can customize the loader with low level APIs.

constructor

  • {String} baseDir - current directory of application
  • {Object} app - instance of egg application
  • {Object} plugins - merge plugins for test
  • {Logger} logger - logger instance,default is console

High Level APIs

async loadPlugin

Load config/plugin.ts

async loadConfig

Load config/config.ts and config/{serverEnv}.ts

If process.env.EGG_APP_CONFIG is exists, then it will be parse and override config.

async loadController

Load app/controller

async loadMiddleware

Load app/middleware

async loadApplicationExtend

Load app/extend/application.ts

async loadContextExtend

Load app/extend/context.ts

async loadRequestExtend

Load app/extend/request.ts

async loadResponseExtend

Load app/extend/response.ts

async loadHelperExtend

Load app/extend/helper.ts

async loadCustomApp

Load app.ts, if app.ts export boot class, then trigger configDidLoad

async loadCustomAgent

Load agent.ts, if agent.ts export boot class, then trigger configDidLoad

async loadService

Load app/service

Low Level APIs

getServerEnv()

Retrieve application environment variable values via serverEnv. You can access directly by calling this.serverEnv after instantiation.

serverEnvdescription
defaultdefault environment
testsystem integration testing environment
prodproduction environment
locallocal environment on your own computer
unittestunit test environment

getEggPaths()

To get directories of the frameworks. A new framework is created by extending egg, then you can use this function to get all frameworks.

getLoadUnits()

A loadUnit is a directory that can be loaded by EggLoader, cause it has the same structure.

This function will get add loadUnits follow the order:

  1. plugin
  2. framework
  3. app

loadUnit has a path and a type. Type must be one of those values: app, framework, plugin.

{
  path: 'path/to/application',
  type: 'app'
}

getAppname()

To get application name from package.json

appInfo

Get the infomation of the application

  • pkg: package.json
  • name: the application name from package.json
  • baseDir: current directory of application
  • env: equals to serverEnv
  • HOME: home directory of the OS
  • root: baseDir when local and unittest, HOME when other environment

async loadFile(filepath)

To load a single file. Note: The file must export as a function.

async loadToApp(directory, property, LoaderOptions)

To load files from directory in the application.

Invoke this.loadToApp('$baseDir/app/controller', 'controller'), then you can use it by app.controller.

async loadToContext(directory, property, LoaderOptions)

To load files from directory, and it will be bound the context.

// define service in app/service/query.ts
export default class Query {
  constructor(ctx: Context) {
    super(ctx);
    // get the ctx
  }

  async get() {}
}

// use the service in app/controller/home.ts
export default async (ctx: Context) => {
  ctx.body = await ctx.service.query.get();
};

async loadExtend(name, target)

Loader app/extend/xx.ts to target, For example,

await this.loadExtend('application', app);

LoaderOptions

| Param | Type | Description | | ----------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------- | | directory | String/Array | directories to be loaded | | target | Object | attach the target object from loaded files | | match | String/Array | match the files when load, default to **/*.js(if process.env.EGG*TYPESCRIPT was true, default to [ '\*\*/\_.(js | ts)', '!\*_/_.d.ts' ]) | | ignore | String/Array | ignore the files when load | | initializer | Function | custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an options object that contain path | | caseStyle | String/Function | set property's case when converting a filepath to property list. | | override | Boolean | determine whether override the property when get the same name | | call | Boolean | determine whether invoke when exports is function | | inject | Object | an object that be the argument when invoke the function | | filter | Function | a function that filter the exports which can be loaded |

Timing

EggCore record boot progress with Timing, include:

  • Process start time
  • Script start time(node don't implement an interface like process.uptime to record the script start running time, framework can implement a prestart file used with node --require options to set process.scriptTime)
  • application start or agent start time
  • Load duration
  • require duration

start

Start record a item. If the item exits, end the old one and start a new one.

  • {String} name - record item name
  • {Number} [start] - record item start time, default is Date.now()

end

End a item.

  • {String} name - end item name

toJSON

Generate all record items to json

  • {String} name - record item name
  • {Number} start - item start time
  • {Number} end - item end time
  • {Number} duration - item duration
  • {Number} pid - pid
  • {Number} index - item index

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

Contributors

Made with contributors-img.

版本与发布记录

@eggjs/core 共发布 95 个版本,首次发布于 2024/06/17;过去 12 个月发布了 33 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.1.0latest2026/10/071 个月前
7.0.22026/10/061 个月前
7.0.2-rc.1rc2026/10/061 个月前
7.0.2-rc.02026/09/231 个月前
7.0.2-beta.27beta2026/09/201 个月前
7.0.2-beta.262026/08/073 个月前

关于 @eggjs/core 的常见问题

@eggjs/core 是什么?

A core plugin framework based on @eggjs/koa

如何安装 @eggjs/core?

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

@eggjs/core 支持 TypeScript 吗?

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

@eggjs/core 支持 ESM 还是 CommonJS?

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

@eggjs/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@eggjs/core 可以商用吗?

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

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

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

@eggjs/core 还在维护吗?

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