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)

Most upvoted comments

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:

$ deno --version
deno 1.15.3 (release, x86_64-unknown-linux-gnu)
v8 9.5.172.19
typescript 4.4.2

Running the program via run works fine.

Compiling works fine with:

deno compile --allow-all --output ./bin/lil ./src/mod.ts

Running the binary I get:

$ ./bin/lil
error: Uncaught SyntaxError: Identifier 'n1' has already been declared
    at file://$deno$/bundle.js:37856:7097

I just run into this myself. Trying to compile an Oak project with deno compile --allow-all -o server server.ts everything appears to work fine. I get no errors compiling. But trying to run the executable I get the following error:

error: ReferenceError: Cannot access 'Response' before initialization
    at file://$deno$/bundle.js:15353:21

Following @CanRau I run deno bundle server.ts server.all.ts and then deno run --allow-all server.all.ts which gives me the same

error: Uncaught ReferenceError: Cannot access 'Response' before initialization
const DomResponse = Response;
                    ^

Running

deno 1.15.2 (release, x86_64-unknown-linux-gnu) (WSL2)
v8 9.5.172.19
typescript 4.4.2

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/node

I 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 --unstable is causing this?

Trying to run the output of deno bundle gives me this:

error: Uncaught (in promise) ReferenceError: Cannot access 'NodeTypeError' before initialization
class ERR_INVALID_ARG_TYPE extends NodeTypeError {
                                   ^
    at file:///Users/...js/tg.bundle.js:12236:36

and indeed, I can see the class NodeTypeError is extended before it has been declared.

% deno --version
deno 1.17.0+ac06797 (canary, x86_64-apple-darwin)
v8 9.7.106.15
typescript 4.5.2

Any pointers would be greatly appreciated.

Seems like Oak’s Response class is colliding with the native Response API.

In the source:

export const DomResponse: typeof Response = Response;

https://github.com/oakserver/oak/blob/6f680c9350e0fd36b99de60aeaaa39552315d2d0/http_server_native.ts#L10

export class Response {

https://github.com/oakserver/oak/blob/6f680c9350e0fd36b99de60aeaaa39552315d2d0/response.ts#L130

In the bundle:

// Line 15426
const DomResponse = Response;

// Line 17038
class Response {

Not sure why this happens.