Skip to content

[p5.js 2.0 Beta Bug Report]: Output a friendly error when p5 is loaded twice on a page #7687

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

Open
1 of 17 tasks
davepagurek opened this issue Mar 29, 2025 · 11 comments
Open
1 of 17 tasks

Comments

@davepagurek
Copy link
Contributor

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.0 beta 5

Web browser and version

Firefox

Operating system

MacOS

Steps to reproduce this

Steps:

  1. Go to the p5 website repo
  2. Switch the 2.0 branch
  3. Run npm run dev
  4. Open the reference for e.g. circle at http://localhost:4321/reference/p5/circle/

You see this error in the console
Image

Not sure what the cause is just yet. Doesn't seem to affect the examples though.

@VANSH3104
Copy link
Contributor

@davepagurek can i give it a try

@davepagurek
Copy link
Contributor Author

Sure thing @VANSH3104, even without a fix yet, let me know in a comment here if you can figure out what the cause is! Now that we've started testing out a beta site for 2.0, you can see this happening for yourself at e.g. https://beta.p5js.org/reference/p5/arc/ and looking in your browser console.

@VANSH3104
Copy link
Contributor

@davepagurek I checked it out in the browser console, and it seems like the issue is related to how width is defined. The error Uncaught TypeError: can't redefine non-configurable property "width" suggests that width might already be locked, possibly by another script or browser restrictions. I also noticed multiple instances of p5.js being loaded, which I think could be causing conflicts.
whats your thought ?

@davepagurek
Copy link
Contributor Author

Thanks @VANSH3104! I think you're right, the script tag is getting included multiple times on the reference page, which I can see in the browser's element inspector dev tools:

Image

So I think there are actually two problems going on here:

  1. (For this repo) When there is already a p5 instance running, rather than outputting a friendly error indicating that p5 is loaded twice, we're getting a cryptic error about property redefinition. Maybe we can detect some other way when p5 is loaded twice (e.g. window.p5 exists already?) and halt initialization with a descriptive message?

  2. (For the p5.js-website repo) Stop p5 from being loaded multiple times on reference pages.

@davepagurek davepagurek changed the title [p5.js 2.0 Beta Bug Report]: "TypeError: can't redefine non-configurable property width" logged when running examples on the site [p5.js 2.0 Beta Bug Report]: Output a friendly error when p5 is loaded twice on a page Apr 3, 2025
@davepagurek
Copy link
Contributor Author

I renamed this issue and made an issue for the website repo here: processing/p5.js-website#770

@davepagurek
Copy link
Contributor Author

More context on the issue here:

  • We detect double-initialization by checking for window._setupDone:
    if (typeof window._setupDone !== 'undefined') {
  • We still set it:
    this._setupDone = true;
    • ...but we don't put anything onto window that starts with an underscore:

      p5.js/src/core/main.js

      Lines 143 to 144 in 3e2d17a

      if(p[0] === '_') continue;
      bindGlobal(p);
    • ...but it this is only set after setup finishes, and we might do the duplicate variable checks before that

So I think we maybe need to update it to look for a different variable that we can set immediately once we know we're in global mode. Maybe we make a window._p5Initialized?

@VANSH3104
Copy link
Contributor

@davepagurek Have you found anything else on the double-initialization issue? The changes window._p5Initialized don’t seem to work as expected.

@davepagurek
Copy link
Contributor Author

@VANSH3104 I haven't tried it out myself, if you've started and want some help debugging feel free to open a draft PR and I can take a look!

@limzykenneth
Copy link
Member

The error logged is because bindGlobal attaches the p5 global mode variable to window using Object.defineProperty(), which by default has configurable set to false which means future attempt at trying to do the same thing will fail with that error.

When p5.js is loaded multiple times, the global initialization is called multiple times, ie. fulfilling the error condition above. In 1.x there are checks to prevent multiple initialization I think but I must have accidentally removed it if it existed. We can reimplement it by checking for the existence of window.p5 and halt initialization if the value is already defined. _setupDone() or any checking of full initialization run the risk of being detected too late because of async loading and out of order script tag execution so I think window.p5 is likely more guaranteed to detect previous import of p5.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
@davepagurek @limzykenneth @VANSH3104 and others