N
Nuxt4mo ago
Fanreza

cant use node-cron in server plugins

// server/plugins/init.ts
import cron from "node-cron";

export default defineNitroPlugin(() => {
console.log("Initializing server plugins...");
console.log("Starting scheduler...");

try {
const task = cron.schedule("0 0 * * *", async () => {
console.log("🔄 Running daily cron job at 00:00 UTC");
});
} catch (error) {
console.error(error);
}
});
// server/plugins/init.ts
import cron from "node-cron";

export default defineNitroPlugin(() => {
console.log("Initializing server plugins...");
console.log("Starting scheduler...");

try {
const task = cron.schedule("0 0 * * *", async () => {
console.log("🔄 Running daily cron job at 00:00 UTC");
});
} catch (error) {
console.error(error);
}
});
when i run this, nothing happen, no error but the server is stoped automatically when i check on website there error saying
TypeError {▼
name: 'TypeError',
message: 'Class extends value [object Module] is not a constructor or null',
stack: 'Class extends value [object Module] is not a constructor or null\n' +
'at D:/On Going Project/hanametrics/node_modules/node-cron/dist/esm/tasks/inline-scheduled-task.js:14:35)\n' +
' at ModuleJob.run (node:internal/modules/esm/module_job:222:25)\n' +
' at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)\n' +
' at async loadESM (node:internal/process/esm_loader:28:7)\n' +
' at async handleMainPromise (node:internal/modules/run_main:113:12)',
unhandled: false,
[Symbol(Symbol.toStringTag)]: 'Error',
}
TypeError {▼
name: 'TypeError',
message: 'Class extends value [object Module] is not a constructor or null',
stack: 'Class extends value [object Module] is not a constructor or null\n' +
'at D:/On Going Project/hanametrics/node_modules/node-cron/dist/esm/tasks/inline-scheduled-task.js:14:35)\n' +
' at ModuleJob.run (node:internal/modules/esm/module_job:222:25)\n' +
' at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)\n' +
' at async loadESM (node:internal/process/esm_loader:28:7)\n' +
' at async handleMainPromise (node:internal/modules/run_main:113:12)',
unhandled: false,
[Symbol(Symbol.toStringTag)]: 'Error',
}
right now im using task from nitro but i wonder why i cant use node-cron
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @Fanreza. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error you’re encountering when using node-cron in a Nuxt server plugin is due to incompatibility between node-cron and the Nitro server engine that powers Nuxt 3. Nitro produces a standalone server output and has its own way of handling scheduled tasks, which is not compatible with traditional Node.js-only libraries like node-cron. The stack trace:
TypeError: Class extends value [object Module] is not a constructor or null
TypeError: Class extends value [object Module] is not a constructor or null
indicates an issue with how node-cron is being imported or executed in the Nitro context. This is a common problem when using Node.js-specific packages in Nitro, which is designed to be cross-platform and serverless-friendly, and does not guarantee compatibility with all Node.js APIs or packages. Recommended Approach:
Instead of using node-cron, you should use Nitro’s built-in scheduled tasks feature. Nitro provides a native way to define scheduled tasks in your Nuxt app, which is compatible with its server engine. Here’s how you can define a scheduled task: 1. Enable tasks in your nuxt.config.ts:
export default defineNuxtConfig({
nitro: {
experimental: {
tasks: true,
},
scheduledTasks: {
'0 0 * * *': 'daily-task', // Runs daily at midnight
},
},
})

export default defineNuxtConfig({
nitro: {
experimental: {
tasks: true,
},
scheduledTasks: {
'0 0 * * *': 'daily-task', // Runs daily at midnight
},
},
})

2. Create the task file in server/tasks/daily-task.ts:
export default defineTask({
meta: {
name: 'daily-task',
description: 'Runs every day at midnight',
},
async run() {
console.log('🔄 Running daily cron job at 00:00 UTC');
// Your logic here
return { result: 'Success' }
}
})

export default defineTask({
meta: {
name: 'daily-task',
description: 'Runs every day at midnight',
},
async run() {
console.log('🔄 Running daily cron job at 00:00 UTC');
// Your logic here
return { result: 'Success' }
}
})

This approach is fully supported by Nitro and avoids the compatibility issues you’re seeing with node-cron. For more details and a real-world example, see the related discussion and code in this GitHub issue about Nitro tasks and jsdom. Summary:
You cannot use node-cron in Nuxt 3 server plugins because Nitro is not a pure Node.js environment. Use Nitro’s scheduled tasks instead—they are designed for this purpose and are fully compatible with Nuxt 3 and Nitro’s deployment targets. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@462986217164636180> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?