skymethod / denoflare

Develop, test, and deploy Cloudflare Workers with Deno.
https://denoflare.dev
MIT License
706 stars 35 forks source link

Error running script Error: Bad module.default: undefined #38

Open otherjohn opened 1 year ago

otherjohn commented 1 year ago

Hi, I am getting the following error when doing : "denoflare serve main"

Compiling https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli-webworker/worker.ts into worker contents... Bundled https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli-webworker/worker.ts (process) in 105ms runScript: /src/index.ts Bundled /src/index.ts (process) in 38ms worker: start Error running script Error: Bad module.default: undefined at Function.create (blob:null/4f3c49d7-43b6-4992-a2f6-11631123b713:578:49) at async Function.start (blob:null/4f3c49d7-43b6-4992-a2f6-11631123b713:679:50) at async blob:null/4f3c49d7-43b6-4992-a2f6-11631123b713:2781:22 at async RpcChannel.receiveMessage (blob:null/4f3c49d7-43b6-4992-a2f6-11631123b713:39:36) at async self.onmessage (blob:null/4f3c49d7-43b6-4992-a2f6-11631123b713:2810:13) error: Failed at the denoflare script

Deno 1.28.3 denoflare v0.5.11

scripts.ts:

export default {
    "scripts": {
      "denoflare": "deno run --unstable --allow-run --allow-write --allow-read --allow-net --allow-env https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli.ts"
    }
};

.denoflare

{
  "$schema": "https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/config.schema.json",
  "scripts": {
    "main": {
      "path": "/src/index.ts",
      "localPort": 3838
    }
  },
  "profiles": {
    "myprofile": {
      "accountId": "xxxxxxxxxxxx",
      "apiToken": "xxxxxxxxx"
    }
  }
}

index.ts

console.log('hello world');
johnspurlock-skymethod commented 1 year ago

There's been a regression after one of the recent Deno version upgrades, where sometimes an error in the script bubbles up as that rather unhelpful: Bad module.default: undefined

As a workaround, to try and see the actual underlying error, set the "localIsolation" to "none" (config property at the script level, ie a peer to "path" in your config.

After typing all of this, though, I think I see the issue: the "path" property value when pointing to a local file and not a url needs to be a full absolute path

otherjohn commented 1 year ago

Ok, I added the "localIsolation":"none" to the .denoflare file. And I think you are right, looking at the "Download" below is a web address, and not local.

This is what I get before fixing the path

>denoflare serve main  
Download ⠋ https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_s
Download ⠙ https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_s
Download ⠹ https://raw.githubusercontent.com/Users/John/xxxx/xxxxs/deno-worker/src/first_s
Download ⠸ https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_s
error: Uncaught (in promise) TypeError: Module not found "https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_steps.ts".
        const module = await import(scriptPath);
                       ^
    at async Function.create (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/module_worker_execution.ts:19:24)
    at async Function.start (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/worker_execution.ts:39:50)
    at async createLocalRequestServer (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_serve.ts:154:20)
    at async Object.serve [as handler] (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_serve.ts:190:32)
    at async CliCommand.routeSubcommand (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_command.ts:104:13)
    at async https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli.ts:33:5

And after I fix the path by setting it to "./src/first_steps", it is the same thing but without the "Download"

>denoflare serve main                                                     
error: Uncaught (in promise) TypeError: Module not found "https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/src/first_steps.ts".
        const module = await import(scriptPath);
                       ^
    at async Function.create (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/module_worker_execution.ts:19:24)
    at async Function.start (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/worker_execution.ts:39:50)
    at async createLocalRequestServer (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_serve.ts:154:20)
    at async Object.serve [as handler] (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_serve.ts:190:32)
    at async CliCommand.routeSubcommand (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_command.ts:104:13)
    at async https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli.ts:33:5

Still not sure hot to fix this though.

johnspurlock-skymethod commented 1 year ago

needs to be a full absolute path

Local file paths need to be absolute if using config: e.g. /Users/whatever/path/to/worker.ts

otherjohn commented 1 year ago

I get the exact same error even when I do my path like /Users/John/xxxx/xxxx/deno-worker/src/first_steps.ts

> denoflare serve main
Download ⠋ https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_s
Download ⠙ https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_s
error: Uncaught (in promise) TypeError: Module not found "https://raw.githubusercontent.com/Users/John/xxxx/xxxx/deno-worker/src/first_steps.ts".
        const module = await import(scriptPath);
                       ^
    at async Function.create (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/module_worker_execution.ts:19:24)
    at async Function.start (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/worker_execution.ts:39:50)
    at async createLocalRequestServer (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_serve.ts:154:20)
    at async Object.serve [as handler] (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_serve.ts:190:32)
    at async CliCommand.routeSubcommand (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_command.ts:104:13)
    at async https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli.ts:33:5
otherjohn commented 1 year ago

I may have found a clue.

If I run denoflare serve main I get the error, but if I run denoflare serve ./src/first_steps.ts it runs without error.

So it seems that something is up with it reading the path in the .denoflare config file.

This is my .denoflare config

{
  "$schema": "https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/config.schema.json",
  "scripts": {
    "main": {
      "path": "/Users/John/xxxx/xxxx/deno-worker/src/first_steps.ts",
      "localIsolation": "none"
    }
  },
  "profiles": {
    "myprofile": {
      "accountId": "xxxxxxxxxxxx",
      "apiToken": "xxxxxxxxx"
    }
  }
}
john-tmc commented 1 year ago

Hi! @otherjohn, I stumbled upon this thread while debugging mine.

The issue I had was using the node library planetscale/database , as shown on the link. : How to use Planetscale with Deno

import { connect } from "npm:@planetscale/database@^1.6";

which ran with no issue locally (deno run), but it did using wrangler serve. Moving it to ESM solved the issue for me.

import { connect } from "https://esm.sh/@planetscale/database@1.6.0";

I doubt it's a version thing, since I've tried lowering it down .1 till 1.0 (using npm) but reached the same issue with wrangler. I don't know if its related to the bug you are seeing, but I found the same error you had, and i just started deleting imports till I found which one was causing it, because I wasn't seeing it on the debug log.