You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法注册hapi-cron-job插件,报错'plugin.register'必须为函数类型

解决Hapi中hapi-cron-job插件注册失败的问题

错误原因

你遇到的"plugin.register" must be of type function错误,是因为hapi-cron-job v2.0.1本身导出的是一个完整的Hapi插件对象(包含register函数、插件名称等属性),但你在自定义的插件配置里,把这个完整对象直接赋值给了register字段,导致Hapi期望该字段是一个函数,实际却传入了一个对象,从而触发报错。

修正方案

调整你的插件配置结构,使用Hapi支持的{ plugin: 插件对象, options: 配置项 }格式来注册插件:

修改hapi-cron-job-plugin.js

const hapiCronJob = require('hapi-cron-job');

module.exports = {
  plugin: hapiCronJob,
  options: {
    jobs: [
      {
        name: "display time", // 修正拼写错误:diplay → display
        enabled: true,
        immediate: true,
        schedule: "every 1 s",
        execute: alertSystemAdminAndDispatcher,
        environments: ['development', 'staging']
      }
    ]
  }
};

额外注意事项

  • 确保alertSystemAdminAndDispatcher函数已经在当前文件中被正确定义或导入,否则定时任务执行时会出现函数未找到的错误
  • 如果你使用的Hapi版本是v18+,这种{ plugin, options }的注册格式是完全兼容的

内容的提问来源于stack exchange,提问作者Pradeep Yenkuwale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 12:18:07