Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Cron decorator throws: TypeError: cron_1.CronJob is not a constructor #12

Closed
ivanjd opened this issue Jan 3, 2020 · 1 comment
Closed

Comments

@ivanjd
Copy link

ivanjd commented Jan 3, 2020

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

By following the example here: https://docs.nestjs.com/techniques/task-scheduling I have code that looks like that:

@Injectable()
export class TestCron {
  private readonly logger = new Logger(TestCron.name);

  @Cron('45 * * * * *')
  handleCron() {
    this.logger.debug('Called when the current second is 45');
  }
}

But I get the following error

(node:20320) UnhandledPromiseRejectionWarning: TypeError: cron_1.CronJob is not a constructor
    at ...\node_modules\@nestjs\schedule\dist\scheduler.orchestrator.js:58:29

Expected behavior

No error should be thrown. In fact if I use @Interval instead of @Cron it works as expected:

 @Interval(1000)
  handleCron() {
    this.logger.debug('Called when the current second is 45');
  }
[Nest] 23620   - 2020-01-03 14:35:18   [TestCron] Called when the current second is 45
[Nest] 23620   - 2020-01-03 14:35:19   [TestCron] Called when the current second is 45
[Nest] 23620   - 2020-01-03 14:35:20   [TestCron] Called when the current second is 45

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


    "@nestjs/common": "^6.10.13",
    "@nestjs/config": "0.0.7",
    "@nestjs/core": "^6.10.13",
    "@nestjs/schedule": "^0.1.1",
@ivanjd ivanjd closed this as completed Jan 3, 2020
@ivanjd
Copy link
Author

ivanjd commented Jan 3, 2020

It turned out that the problem is not coming from the lib, but it is something that other people could run into so I will post a short explanation:

We use tsconfig-paths (https://www.npmjs.com/package/tsconfig-paths) to be able to use module paths relative to the project root. In the project root, however, we have a folder called cron where we keep a set ot cron classes that hold cron methods (as opposed to having cron methods in the service classes). All this classes are exported through a cron/index.ts file and imported as:
import cron from 'cron'.
The lib, however, also uses a module called cron, and when it imports it the resolver imports our cron module instead of the proper dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant