# P5.js Web Editor problem exporting a function

**URL:** https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420
**Category:** Beginners
**Created:** [December 2, 2024, 7:41pm UTC](https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420 "2024-12-02T19:41:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![edufissure](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@edufissure](https://discourse.processing.org/u/edufissure)
#### Post date: [December 2, 2024, 7:41pm UTC](https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420/1 "2024-12-02T19:41:08Z")

</div>

Hi im doing this test to import a class from a separate file:

> **[p5.js Web Editor](https://editor.p5js.org/egutie9/full/f_Of3FXPS)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

but i got error:

The export:

 ![imagen](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/3/531a3a2a0fcc7188f05d03ffed3a2bb8418685e7.png)

The import:

 ![imagen](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/1/41785bd66c7337155a408b8bd9f44d8d5fbc07e3.png)

The html file:

 ![imagen](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/3/c39c4326b4fcc54c830e5cf683251e00c97da844.png)

Any help would be apreciated

This is for educational purpose

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [December 3, 2024, 3:20am UTC](https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420/2 "2024-12-03T03:20:55Z")

</div>

Do you have to use ‘module’? I added this line to index.html  
`<script src="Pacman.js"></script>`  
and did _not_ place the file in a folder. I also removed the word ‘export’ before class Pacman{…}

 ![we](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/4/44cba10b0d02cc58379f1433b7543bb8e918b020.png)

I used this code without _import_

```auto
let myPacman = new Pacman(100,100);
console.log(Pacman);
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  myPacman.showPacman();
}

```

It worked and I got this output:

 ![pman](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/a/1ae52a97bef2a2d60ee2ee3256567ec95e9d3094.png)

---

<div class="post-metadata">

### Author: ![edufissure](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@edufissure](https://discourse.processing.org/u/edufissure)
#### Post date: [December 3, 2024, 10:14am UTC](https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420/3 "2024-12-03T10:14:49Z")

</div>

Also got it, but i need inside folder classes, as id have more classes and i want the project some kind of order.  
In JS all files that has any import/export should be modules.

Also import and export should be as all JS works this way.

I think import/export doesnt work in p5.js…dont know if its a bug, or is not implemented yet, or you have to import/export like you did @svan…

Thanks, hope anybody specially dev could give a light in the darkness. 😉

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 3, 2024, 1:36pm UTC](https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420/4 "2024-12-03T13:36:04Z")

</div>

> [@edufissure](#):
>
> I think `import` / `export` don’t work in p5.js…

Those 2 keywords are JS features, and p5.js, which is a frontend library, can’t change that:

> [@Import class other file syntax error](https://discourse.processing.org/t/import-class-other-file-syntax-error/45358/12):
>
> Your “Calculadora.zip” file had to many things in it. I’ve removed most of it and edited the rest. Also, I’ve created a GitHub repo for it here: You can also check it running online on this link:

> <https://github.com/GoToLoop/P5-JS-ECMA-Module-Demo/blob/main/src/sketch.mjs>

> <https://github.com/GoToLoop/P5-JS-ECMA-Module-Demo/blob/main/src/classes/Animal.mjs>

> [@edufissure](#):
>
> … don’t know if it’s a bug, or is not implemented yet,

As I have already mentioned to you on your previous forum thread:

> [@Import class other file syntax error](https://discourse.processing.org/t/import-class-other-file-syntax-error/45358/9):
>
> p5.js itself is a regular .js library file, not a module 1.
> 
> Indeed they’ve never yet cared about using ECMA module (.mjs) files in the browser!
> 
> But weirdly, they do care about Node.JS’ Common module (.cjs) files, even though p5.js isn’t a backend library, but a frontend 1!

B/c they don’t even acknowledge ECMA Script Modules (ESM / “.mjs”) exist anywhere on their docs, it’s no wonder their “p5.js Web Editor” doesn’t seem to be compatible w/ ESM either!

Pretty much any other JS code hosting/editor solution available should work!

For example, you can try out:

- [OpenProcessing.org](http://OpenProcessing.org)
- [Glitch.com](http://Glitch.com)
- [GitHub.com](http://GitHub.com)

And many others to host JS code + p5.js library!

---

<div class="post-metadata">

### Author: ![ProcessingOrg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/processingorg/32/18341_2.png) [@ProcessingOrg](https://discourse.processing.org/u/ProcessingOrg)
#### Post date: [December 3, 2024, 2:07pm UTC](https://discourse.processing.org/t/p5-js-web-editor-problem-exporting-a-function/45420/5 "2024-12-03T14:07:33Z")

</div>

Thanks for sharing your thoughts! p5.js and the p5.js web editor are open-source projects built and maintained by the community. If there’s a feature you’d like to see or something that could be improved, I’d encourage you to open an issue on the [p5.js repository](https://github.com/processing/p5.js/issues) or the [p5.js web editor repository](https://github.com/processing/p5.js-web-editor/issues) on GitHub, or even contribute a code change. Your feedback and contributions are what help these projects grow and evolve 😊
