Skip to content

hywax/nuxt-cron

Repository files navigation

nuxt-cron

Nuxt Cron

npm version npm downloads License

A Nuxt module for cron jobs in your app.

Features

  • 👌  19 time presets
  • 🕔️  Time zone support
  • 🪄️  TypeScript support
  • ✨  Auto imports enabled
  • ⚡  Zero configuration to start
  • 🤝  Nuxt 3/4 support
  • 📦  Extendable by Nuxt modules

Installation

# Using pnpm
pnpm add nuxt-cron -D

# Using yarn
yarn add nuxt-cron -D

# Using npm
npm install nuxt-cron -D

Usage

Setup

Add nuxt-cron to the modules section of nuxt.config.ts

export default defineNuxtConfig({
  modules: ['nuxt-cron']
})

Configuration

You can configure the module by adding a nuxt-cron section to your nuxt.config file.

export default defineNuxtConfig({
  cron: {
    runOnInit: true,
    timeZone: 'Africa/Abidjan',
    jobsDir: 'cron'
  }
})

by default, nuxt-cron will auto-import your cron from the models directory from server directory. You can change this behavior by setting the jobsDir option.

API

defineCronHandler

This function creates a new cron job. Example usage:

// server/cron/job.ts
import { defineCronHandler } from '#nuxt/cron'

export default defineCronHandler('everySecond', () => {
  console.log('I run every seconds')
})

or use with params:

// server/cron/job.ts
import { defineCronHandler } from '#nuxt/cron'

export default defineCronHandler('everySecond', () => {
  console.log('I run every seconds')
}, { runOnInit: true })

More examples can be found here.

Credits

A huge thank you to everyone who is helping to improve nuxt-cron. Thanks to you, the project can evolve!

Contributors

To become a contributor, please follow our contributing guide.

nuxt-cron

License

This app is open-sourced software licensed under the MIT license.