deno: Cannot access 'Response' before initialization on `deno bundle`
After upgrading to version 1.14 this error happened when using deno bundle
Example test.ts code:
import { Application } from "https://deno.land/x/oak@v9.0.0/mod.ts";
const app = new Application();
app.use((ctx) => {
ctx.response.body = "Hello World!";
});
await app.listen({ port: 8000 });
Commads:
deno bundle test.ts out.js
deno run -A out.js
Error:
error: Uncaught ReferenceError: Cannot access 'Response' before initialization
const DomResponse = Response;
^
Deno version
deno 1.14.0 (release, x86_64-apple-darwin)
v8 9.4.146.15
typescript 4.4.2
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 14
- Comments: 16 (5 by maintainers)
At least in my case, this bug is fixed in a newer version of swc (1.2.102+). It sounds like someone is actively working on upgrading the version of swc used in Deno.
More info:
In my case (which I believe is the same situation as in this bug), I ran swc’s spack command manually and found that the version built into Deno 1.16.2 (swc bundler 0.75.0, from swc 1.2.101) exhibited this problem.
The very next release of swc (swc bundler 0.75.1 from swc 1.2.102) fixed my issue. I also confirmed this issue remained fix on the latest release: @swc/core@1.2.112.
Note that I don’t know how Deno configures swc, so I’m not sure if it’s possible to bundle something up yourself using swc directly (without Deno) and then have it work with
deno compile.I’m also hitting a similar issue:
Running the program via run works fine.
Compiling works fine with:
Running the binary I get:
I just run into this myself. Trying to compile an Oak project with
deno compile --allow-all -o server server.tseverything appears to work fine. I get no errors compiling. But trying to run the executable I get the following error:Following @CanRau I run
deno bundle server.ts server.all.tsand thendeno run --allow-all server.all.tswhich gives me the sameRunning
It wasn’t updated for 1.14.2. I will check to see if swc updates have fixed it though.
@rabbitkiller-dev the fix is not in 1.16.4. You need to either use the canary release or wait until 1.17.0 next week. We close issues when the fix is merged into main, not when it becomes part of a release.
@emrul please open a new issue and provide some reproduction code. This might actually be a problem in
deno_std/nodeI have tried the latest Deno release (1.17.0) and also tried canary and I am facing what appears to be the same issue on my script - both with Deno bundle and Deno compile. The script works fine when executed via
deno run. I am not sure if the fact that I need--unstableis causing this?Trying to run the output of
deno bundlegives me this:and indeed, I can see the class
NodeTypeErroris extended before it has been declared.Any pointers would be greatly appreciated.
Seems like Oak’s Response class is colliding with the native Response API.
In the source:
https://github.com/oakserver/oak/blob/6f680c9350e0fd36b99de60aeaaa39552315d2d0/http_server_native.ts#L10
https://github.com/oakserver/oak/blob/6f680c9350e0fd36b99de60aeaaa39552315d2d0/response.ts#L130
In the bundle:
Not sure why this happens.