YMFE/yapi · error
不存在的hookname
Error message
不存在的hookname
What it means
bindHook validates the name against the known hooks map; if the hookname is not registered it throws '不存在的hookname'. YApi only supports a fixed set of hook points, so plugins must bind to existing ones.
Source
Thrown at server/plugin.js:197
/**
* addNoticePlugin(config)
*
* config.weixin = {
* title: 'wechat',
* hander: (emails, title, content)=> {...}
* }
*/
addNotice:{
type: 'multi',
listener: []
}
};
function bindHook(name, listener) {
if (!name) throw new Error('缺少hookname');
if (name in hooks === false) {
throw new Error('不存在的hookname');
}
if (hooks[name].type === 'multi') {
hooks[name].listener.push(listener);
} else {
if (typeof hooks[name].listener === 'function') {
throw new Error('重复绑定singleHook(' + name + '), 请检查');
}
hooks[name].listener = listener;
}
}
/**
*
* @param {*} hookname
* @return promise
*/
function emitHook(name) {
if (hooks[name] && typeof hooks[name] === 'object') {View on GitHub (pinned to 59bade3a8a)
Solutions
- Use an existing hook name from the hooks registry in server/plugin.js.
- Check YApi version compatibility — hook names change across versions.
- Grep the codebase for 'bindHook(' usages to see valid names.
Example fix
// before
yapi.bindHook('interface_add', handler); // not a real hook
// after
yapi.bindHook('add_router', handler); Defensive patterns
Strategy: validation
Validate before calling
const knownHooks = ['add_router', 'interface_save', 'project_add', 'init', ...]; // from server/plugin.js
if (!knownHooks.includes(name)) throw new Error(`Unknown hook: ${name}`); Type guard
function isRegisteredHook(name, hooks) {
return typeof name === 'string' && name in hooks;
} Try / catch
try {
yapi.bindHook(name, listener);
} catch (e) {
if (e.message === '不存在的hookname') {
console.error(`Hook "${name}" not supported in this YApi version`);
} else { throw e; }
} Prevention
- Copy hook names from the hooks registry in server/plugin.js, not memory.
- Verify hook names when upgrading YApi versions.
- Read existing plugins for canonical hook usage examples.
When it happens
Trigger: yapi.bindHook('some_typo_or_invented_hook', fn) where the name is absent from the hooks object in server/plugin.js.
Common situations: Plugin written for a different YApi version whose hook names differ; misspelled hook name; copying hook names from docs of another project.
Related errors
- 缺少hookname
- 重复绑定singleHook(${name}), 请检查
- config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件
- Plugin Route config Error
- Plugin Route path conflict, please try rename the path
AI-assisted analysis of YMFE/yapi@59bade3a8a (2026-08-29).
Data as JSON: /api/errors/7a733471ba8ee7ea.
Report an issue: GitHub.