Setup method runs twice

My p5js app runs the setup function twice. I have stripped the whole thing down as much as possible, and it’s still happening. This is messing with my app because (a) I wish to run some fairly heavy operations in setup and (b) I add some objects to an array in setup, and if it runs twice then the objects are doubled up.

My index.js looks like this:

const sketch = (processing) => {
  processing.setup = () => {
    console.log('run setup');
const myProcessing = new p5(sketch);

…and my index.html looks like this:

<!DOCTYPE html>
  <meta charset="utf-8">
  <div id="test"></div>
<script src="./lib/p5.js"></script>
<script src="./lib/p5.sound.js"></script>
<script type="module" src="./src/index.js"></script>

Hello and welcome, @dansumption!

I can’t reproduce this locally. setup is only called once when running on p5.js 1.0.0. Could you host a complete example online for us to try? Is the problem present if you try another browser? Which version of p5.js are you on?


Hi Sven,

Thanks for checking this out.

Strange, I have version 1.0.0 here too.

I’ll stick a version online when I get a chance, as well as trying some more browsers. Have been running it in Chrome.

In case anyone else encounters this problem, I finally managed to solve it, and it was down to the way that I wasn instantiating processing and assigning the setup and draw methods. I’ve documented it in this thread: How to access p5js functions when using ES6 modules

1 Like