Registry / web-framework / egg
library0.2.0jsnpmunverified

Egg.js is a robust web framework for Node.js, designed to streamline enterprise-level application development by enforcing conventions and providing a powerful plugin system. Built upon Koa.js, it abstracts away complex middleware logic and offers a multi-process architecture for enhanced stability and scalability. The current stable version is 3.34.0, which regularly receives minor updates, and a major version 4.x is in active beta development, expected around April 2026. Egg.js differentiates itself through its "convention over configuration" approach, extensive plugin ecosystem, and comprehensive built-in features for areas like security, logging, and application lifecycle management, making it particularly suitable for large teams and complex business scenarios requiring high quality and reliability.

npm install egg
INSTALL
IMPORT
SIG · EGG
E
egg
web-frameworkjavascriptv0.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Controller
import { Controller } from 'egg'; // For TypeScript or ESM in Node environments (with transpilation or v4+) const Controller = require('egg').Controller; // CommonJS (v3)
import Controller from 'egg'; // Incorrect default import import { Controller } from '@eggjs/core'; // Incorrect path for v3
Controller is a base class for defining application logic. For v3, CommonJS `require` is typical. v4 is moving towards `@eggjs/*` scoped packages and native ESM support.
Service
import { Service } from 'egg'; // For TypeScript or ESM in Node environments (with transpilation or v4+) const Service = require('egg').Service; // CommonJS (v3)
import Service from 'egg'; // Incorrect default import import { Service } from '@eggjs/core'; // Incorrect path for v3
Service is a base class for encapsulating business logic. Similar to Controller, CommonJS `require` is standard for v3.
Application
import { Application } from 'egg'; // For TypeScript or ESM in Node environments (with transpilation or v4+) const Application = require('egg').Application; // CommonJS (v3) - for programmatic instance const AppBootHook = require('egg').AppBootHook; // CommonJS (v3) - for lifecycle hooks
import { Application } from '@eggjs/core'; // Only for programmatic use in v4 or core package const app = require('egg'); // App object is not the default export
The `Application` class (or `EggCore`) represents the application instance. In typical app development, `app` and `ctx` are accessed via `this.app` and `this.ctx` within Controllers and Services. For lifecycle hooks, `AppBootHook` class is recommended since v3.

Demonstrates how to set up a basic Egg.js application using the recommended scaffold, define a controller, configure routes, and create a simple service.

import { Application } from 'egg'; // Use import for TypeScript or transpiled environments import { Controller } from 'egg'; // 1. Initialize project using scaffold (run in your terminal) // $ npm init egg --type=simple my-egg-app // $ cd my-egg-app // $ npm install // 2. Create app/controller/home.ts (or .js) // A simple controller class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'Hello Egg.js!'; } } // module.exports = HomeController; // For .js files (CommonJS) // 3. Create app/router.ts (or .js) // Define routing export default (app: Application) => { const { router, controller } = app; router.get('/', controller.home.index); }; // module.exports = (app) => { // const { router, controller } = app; // router.get('/', controller.home.index); // }; // For .js files (CommonJS) // 4. Run the application (in your terminal) // $ npm run dev // Then open http://localhost:7001 in your browser. // Example of custom service (app/service/greeter.ts or .js) class GreeterService extends Service { async greet(name: string) { return `Greetings, ${name}!`; } } // Example of using the service in a controller class UserProfileController extends Controller { async show() { const { ctx, service } = this; const userName = ctx.query.name || 'World'; const greeting = await service.greeter.greet(userName); ctx.body = greeting; } } // And add to router.ts: // router.get('/greet', controller.userProfile.show);
Debug
Known issues
breakingEgg.js v4.x will require Node.js >= 18.19.0. If you are using an older Node.js version, you must upgrade before migrating to Egg.js v4.x.
fix
Upgrade your Node.js runtime to version 18.19.0 or higher. For v3, ensure Node.js >= 14.20.0.
affects: >=4.0.0-beta
breakingThe lifecycle event functions (e.g., `app.beforeStart`, `app.ready`, `app.beforeClose`) have been deprecated. Developers should now use class methods within an `AppBootHook` class defined in `app.js` or `agent.js` (e.g., `async willReady()`, `async didReady()`, `async beforeClose()`).
fix
Refactor your application's lifecycle hooks to use the `AppBootHook` class methods as per the documentation for better control and maintainability.
affects: >=3.x
breakingStarting with Egg.js v4.x, many core packages have been renamed from `egg-something` to `@eggjs/something` (e.g., `egg-security` to `@eggjs/security`, `egg-jsonp` to `@eggjs/jsonp`). This requires updating package names in `package.json` and potentially import paths.
fix
During v4 migration, update dependencies in `package.json` and adjust import statements to use the new `@eggjs/` scoped package names.
affects: >=4.0.0-beta
gotchaEgg.js heavily relies on 'convention over configuration'. File names within `app/controller`, `app/service`, etc., are automatically mapped to camelCase properties on `app.controller` or `ctx.service`. For example, `app/service/user_info.js` maps to `ctx.service.userInfo`.
fix
Adhere to the file naming conventions (e.g., use snake_case for filenames that represent multi-word properties) to ensure correct auto-loading and access.
affects: All
gotchaAlways configure `exports.keys` with a strong, secret array of strings in your `config/config.default.js` or environment variables. This is crucial for security-related features like cookie signing and CSRF protection.
fix
Set `exports.keys = ['your_secret_key_1', 'your_secret_key_2'];` with unique, random strings. Do not hardcode secrets in production configurations.
affects: All
Errors
Common errors & fixes
Error: Cannot find module 'egg'
The `egg` package is not installed or not resolvable in the current project.
fix
Run `npm install egg --save` or `pnpm install egg` in your project directory.
TypeError: Cannot read properties of undefined (reading 'Controller')
You are likely trying to access `egg.Controller` but `egg` itself was not properly imported or is undefined. This can happen with incorrect import syntax or if `egg` is not installed.
fix
Ensure `const Controller = require('egg').Controller;` for CommonJS or `import { Controller } from 'egg';` for ESM/TypeScript. Verify `egg` is installed.
Error: Current Node.js version is vX.Y.Z, but Egg.js requires >= v14.20.0.
The Node.js version installed on your system does not meet the minimum requirement for the installed Egg.js version.
fix
Upgrade your Node.js runtime to version 14.20.0 or higher. For Egg.js v4.x, Node.js >= 18.19.0 is required. Use a version manager like `nvm` to switch Node.js versions.
TypeError: this.ctx.render is not a function
You are trying to use `ctx.render` without a configured view plugin, or the view plugin is not correctly enabled.
fix
Install a view plugin like `egg-view-nunjucks` (`npm i egg-view-nunjucks --save`) and enable it in `config/plugin.js`. Configure the view engine in `config/config.default.js`.
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
egg-corerequiredCore functionality for application loading and management.
egg-clusterrequiredProvides multi-process management and cluster capabilities.
koa-bodyparserrequiredCommon middleware for parsing request bodies.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
2
Resources