denoland / denoland/deploy_feedback

[Bug]: "TypeError: Cron cannot be defined in this context" when including slow import

Open
#563 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
79
Forks
5
PR merge metrics
No merged PRs in 30d

Description

### Problem description

I have an issue using `Deno.cron()` on Deploy, where if I import something which takes too much time to import, the cron job is not registered correctly when the program is deployed. Instead I see in the logs that I get this error: "ypeError: Cron cannot be defined in this context. Deno.cron must be called at top-level only."

My `Deno.cron()` call is defined at the top level in my main.ts file, and is registered correctly as soon as I remove the slow import. Other imports which don't take too much time seem to work fine.

My code looks something like this:
```ts
// This import works fine
import { kvdex } from "kvdex"

// This import causes the issue
import "./slow_import"

Deno.cron("name", "0 0 * * *", () => console.log("cron job"))
```

### Steps to reproduce

1. Include a large/slow import
2. Define a cron job at top level
3. Deploy to Deno Deploy

### Expected behavior

I would expect Deno Deploy to be able to properly detect and register a cron job even though the entry point imports something large/slow.

### Environment

_No response_

### Possible solution

One possible workaround is to defer or lazy load imports.

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.