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

Bug: 'Worker is not defined' with Pixi 7.4.0 and Node v18.17.1 #10170

Closed
bhousel opened this issue Feb 1, 2024 · 15 comments · Fixed by #10227
Closed

Bug: 'Worker is not defined' with Pixi 7.4.0 and Node v18.17.1 #10170

bhousel opened this issue Feb 1, 2024 · 15 comments · Fixed by #10227
Assignees

Comments

@bhousel
Copy link
Contributor

bhousel commented Feb 1, 2024

Current Behavior

We have a test suite using Node Test Runner and running with Node v18.17.1 and after installing the latest v7.4.0 Pixi release our tests are throwing a new error:

file:///Users/bryan/Projects/rapid/node_modules/@pixi/assets/lib/_virtual/checkImageBitmap.worker.mjs:20
class WorkerInstance extends Worker {
                             ^

ReferenceError: Worker is not defined
    at file:///Users/bryan/Projects/rapid/node_modules/@pixi/assets/lib/_virtual/checkImageBitmap.worker.mjs:20:30
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

I'm not sure whether we need to switch to a different build of Pixi for node. (I also might be able to work around the issue another way, since we aren't using the worker functionality here.)

Expected Behavior

No errors.

Steps to Reproduce

tbd

Environment

  • pixi.js version: e.g. 7.4.0
  • Browser & Version: e.g. Node v18.17.1

Possible Solution

No response

Additional Information

No response

@bigtimebuddy
Copy link
Member

pixi.js does not run natively in Node.js, it's browser only. If you need to run in Node.js, you can use this project: https://github.com/pixijs/node

@bigtimebuddy bigtimebuddy closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2024
@bhousel
Copy link
Contributor Author

bhousel commented Feb 1, 2024

Thanks @bigtimebuddy , and sorry for the noise..
I tracked it down to an unneeded import * as PIXI from 'pixi.js'; from some code that was expected to run headless.

bhousel added a commit to facebook/Rapid that referenced this issue Feb 1, 2024
We shouldn't be star importing from Pixi in our `util` module,
this is code that should be expected to be run headlessly in unit tests.

re: pixijs/pixijs#10170
@bigtimebuddy
Copy link
Member

No worries!

@shipurjan
Copy link

shipurjan commented Feb 15, 2024

I still have this problem in a Tauri app with NextJS (everything is configured to be client-side). Everything worked on pixi.js 7.3.3, but after updating to 7.4.0 it still works correctly, but I get an error

 node_modules\.pnpm\@pixi+assets@7.4.0_@pixi+core@7.4.0\node_modules\@pixi\assets\lib\_virtual\checkImageBitmap.worker.mjs (20:29) @ Worker
 ⨯ ReferenceError: Worker is not defined
    at __webpack_require__ (C:\Users\niar-windows\Documents\repos\bioparallel\.next\server\webpack-runtime.js:33:42)

Also the project can't be build, because Worker is not defined, and even though everything is configured to be on the client-side it still seems to check if everything functions on SSR or something

EDIT:
I lazy loaded the component using pixi.js with

const Dashboard = dynamic(
    () =>
        import("@/components/tabs/dashboard/dashboard").then(
            mod => mod.Dashboard
        ),
    { ssr: false }
);

And now it seems to work without errors. So it's not a pixi problem

@vincaslt
Copy link

vincaslt commented Feb 15, 2024

I seem to be getting this exact issue on Electron, running on the Main (node) process, and I'm using the node package. Seems like some juggling around worker files might have introduced this issue? pixijs/node#15

Edit: my thinking is after the PR below the worker code is imported where before during the build now, where before it was encoded as string and loaded as-needed. The pixi/node seems to work by letting pixi load whatever extensions it needs and then unloading those that aren't compatible with node. Problem is that the code is not eliminated and the class is created up-front, not on-demand so the loader adds a class that tries to extend Worker class that doesn't exist in node, and the code is run, even though it would get unloaded later in the node package.

@bigtimebuddy bigtimebuddy reopened this Feb 15, 2024
@bigtimebuddy
Copy link
Member

PR that affected Workers is here #9399
@SuperSodaSea do you think you could you take a look at this?

@SuperSodaSea SuperSodaSea self-assigned this Feb 16, 2024
@SuperSodaSea
Copy link
Member

I apologize for not considering Node.JS environment in that PR! I'll fix this issue soon.

@notchris
Copy link

@SuperSodaSea Thank you!

@bigtimebuddy
Copy link
Member

I wonder if we can keep the external worker.ts files (and all the type checking and linting benefits of that), but import and inline the code as a string. So we aren't shipping external worker files.

@notchris
Copy link

@bigtimebuddy Not sure if it's a side effect of manually patching Worker to see if I could get around the issue, but I started getting similar errors about document

@bigtimebuddy
Copy link
Member

@notchris can you call out those document issues? Sure we can get that sorted too.

@notchris
Copy link

@bigtimebuddy Sure thing - it's actually not document, that was my mistake. The issue comes from extract method on app.renderer

/Users/notchris/Desktop/nodesdl-pixi/node_modules/@pixi/extract/src/Extract.ts:173
const imageData = new ImageData(new Uint8ClampedArray(pixels.buffer), width, height);
ReferenceError: ImageData is not defined

@bigtimebuddy
Copy link
Member

@notchris extract.canvas doesn't work in Node. You need to use extract.pixels or extract.base64 instead.

@SuperSodaSea
Copy link
Member

I wonder if we can keep the external worker.ts files (and all the type checking and linting benefits of that), but import and inline the code as a string. So we aren't shipping external worker files.

Currently the worker code is already inlined as a string (see WORKER_CODE in @pixi/assets/lib/_virtual/checkImageBitmap.worker.mjs), so we won't have any extra external worker file (for example, you can see the worker code in dist/pixi.js).

@notchris
Copy link

@notchris extract.canvas doesn't work in Node. You need to use extract.pixels or extract.base64 instead.

I'm referencing the official example: https://github.com/pixijs/node, but that's good to know. Should that be updated as well? I'm going to try out .pixels now!

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

Successfully merging a pull request may close this issue.

6 participants