node-cron: missing dependency: uuid

this is the reference to the module: https://github.com/node-cron/node-cron/blob/master/src/scheduled-task.js#L6

I can not understand how a test can be real when devDependencies are required for running it. I mean,

  1. if I install devDependencies I have nyc, that depends on uuid, and the test will pass
  2. when I run it in real world, with a simple npm i, I have no devDependencies packages, so it really say that uuid is missing.

I have no idea how to post a failing test and a patch fixing it.

The required patch is to add uuid dependency in package.json

For now, my workaround is to add uuid as an explicit dependency in my project.

This is the output of the error after npm i and node index.js:


Error: Cannot find module 'uuid'
Require stack:
- /home/node/code/node_modules/node-cron/src/scheduled-task.js
- /home/node/code/node_modules/node-cron/src/node-cron.js
- /home/node/code/models/cronschedule.js
- /home/node/code/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/home/node/code/node_modules/node-cron/src/scheduled-task.js:6:14)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/home/node/code/node_modules/node-cron/src/node-cron.js:3:23)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/node/code/node_modules/node-cron/src/scheduled-task.js',
    '/home/node/code/node_modules/node-cron/src/node-cron.js',
    '/home/node/code/models/cronschedule.js',
    '/home/node/code/index.js'
  ]
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 26
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

i have the same problem

This issue is already reported here: #355

Same here 😭

Same here

just ran into this issue.

@Juneezee I replaced uuid with crypto.randomUUID as per suggestion from @weyert and submitted another pull-request. I think reducing dependencies is always the better option. Let me know if you have any objections.

same here

Same problem

I have the same problem… (´;ω;`)ウゥゥ

for now I’m using this patch, if anyone is interested

with npm and yarn is different

pnpm: (root of package.json)

"pnpm": {
    "patchedDependencies": {
        "node-cron@3.0.1": "patches/node-cron@3.0.1.patch"
    }
}

in project root create this file patches/node-cron@3.0.1.patch

diff --git a/src/background-scheduled-task/index.js b/src/background-scheduled-task/index.js
index 5e92e2e03e9f4afa9d5f5f9fb08e1e10f8741afd..9c5c04e2ebf692fb25c5c764344ca7bc57a33bbd 100644
--- a/src/background-scheduled-task/index.js
+++ b/src/background-scheduled-task/index.js
@@ -1,7 +1,7 @@
 const EventEmitter = require('events');
 const path = require('path');
 const { fork } = require('child_process');
-const uuid = require('uuid');
+const crypto = require('crypto');
 
 const daemonPath = `${__dirname}/daemon.js`;
 
@@ -17,7 +17,7 @@ class BackgroundScheduledTask extends EventEmitter {
         this.cronExpression = cronExpression;
         this.taskPath = taskPath;
         this.options = options;
-        this.options.name = this.options.name || uuid.v4();
+        this.options.name = this.options.name || crypto.randomUUID();
 
         if(options.scheduled){
             this.start();
diff --git a/src/scheduled-task.js b/src/scheduled-task.js
index 52a1f590011f16513bc399325722c59bcbf4ae17..3d3cbef8d2cc26f36cf3c8bd9e0d83ea6a2f9811 100644
--- a/src/scheduled-task.js
+++ b/src/scheduled-task.js
@@ -3,7 +3,7 @@
 const EventEmitter = require('events');
 const Task = require('./task');
 const Scheduler = require('./scheduler');
-const uuid = require('uuid');
+const crypto = require('crypto');
 
 class ScheduledTask extends EventEmitter {
     constructor(cronExpression, func, options) {
@@ -16,7 +16,7 @@ class ScheduledTask extends EventEmitter {
         }
       
         this.options = options;
-        this.options.name = this.options.name || uuid.v4();
+        this.options.name = this.options.name || crypto.randomUUID();
 
         this._task = new Task(func);
         this._scheduler = new Scheduler(cronExpression, options.timezone, options.recoverMissedExecutions);
diff --git a/src/storage.js b/src/storage.js
index 6c131a4f17569e098493840aa1d58ee95e92df2d..6a5fad0f5da2bfbdb96b7386480dcdb588e200e0 100644
--- a/src/storage.js
+++ b/src/storage.js
@@ -6,9 +6,9 @@ module.exports = (() => {
     return {
         save: (task) => {
             if(!task.options){
-                const uuid = require('uuid');
+                const crypto = require('crypto');
                 task.options = {};
-                task.options.name = uuid.v4();
+                task.options.name = crypto.randomUUID();
             }
             global.scheduledTasks.set(task.options.name, task);
         },