SyntaxError: import declarations may only appear at top level of a module
in Webstorm.
When i have:
sketch.js
import Animal from "path to animal.js";
function setup() {
const dog = new Animal("dog");
dog.sayName();
createCanvas(400, 400);
}
function draw() {
background(220);
}
Both these need to be loaded as modules so in the html file change the ātypeā to āmoduleā
<!-- PLEASE NO CHANGES BELOW THIS LINE (UNTIL I SAY SO) -->
<script language="javascript" type="text/javascript" src="libraries/p5.min.js"></script>
<script language="javascript" type="module" src="animal.js"></script>
<script language="javascript" type="module" src="AnimalDemo.js"></script>
<!-- OK, YOU CAN MAKE CHANGES BELOW THIS LINE AGAIN -->
@GoToLoop thanks for that answer it worked perfectly in VSC.
The Processing IDE doesnāt seem to able to handle user files with the extension .mjs files but your solution still works if you leave the extensions as .js
If theres any dev here, should fix this in a near futureā¦is bad to have this problems to import and export, so common thing, and so standarizedā¦Its weird that p.js works different and lack of documetnation about itā¦( at least i didnt found any)
In your original question, your class Animal is defined inside file " Animal.js".
B/c that class is exported as export default Animal;, you can import it inside your file āsketch.jsā as: import Animal from "./Animal.js";
Assuming both are located in the same folder.
Also, b/c itās a ādefault exportā, you can use any other name in place of Animal: import Pet from "./Animal.js";
As Iāve mentioned before, itās advisable to use the extension ā.mjsā for files that should be loaded as a module.
So, Iād rename āAnimal.jsā to āAnimal.mjsā and āsketch.jsā to āsketch.mjsā.
This way, itās clearer they require special loading strategy: import Animal from "./Animal.mjs";
sure you can download the project ( dont be afraid is where im doing some testsā¦its almost empty)ā¦Im teacher of vocational education in Computer Scienceā¦and im teaching JSā¦another thing as Animal is a classā¦should i use export class Animal ???
With the / doesnāt work, also i have to write type=āmoduleā in other tutorials donāt appear this type only:
<script src="sketch.js"></script>
Also i still think that window.setup = setup and window.draw=draw at the end of sketch.js should no be thereā¦if you export and import a js file, just it, donāt know why window is doing hereā¦
Any dev could fix this ?
Also seem that it has gone the tutorial were you setup your environment, linking html file with sketch.jsā¦by now only appear the p5js web editorā¦but not any explanation about what to write in html file, and how to link to a sketch.js
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:
1 - Your āCalculadora.zipā file had to many things in it.
Iāve removed most of it and edited the rest.
Thanks for your effort, i told you, dont be afraid, most things were from HTML Boilerplate project template in Webstorm.
2 - Why async ? Ive never seen this, if i delete async is working fine in my computer
I prefer to have it downloaded in the project,also Webstorm pop up a warning if library is not downloaded.
The defer also is not necessary no ?
4 - You renamed all to .mjs but according to @quark
The Processing IDE doesnāt seem to able to handle user files with the extension .mjs files but your solution still works if you leave the extensions as .js
async
It makes a <script> tag to load in parallel while the page is still being processed & immediately run it as soon as its loading is finished.
The reason Iāve used the attribute async for āscriptindex.jsā is b/c it doesnāt depend on any other file and no other file depends on it.
Actually, I couldāve used async for the p5.js loading as well in this particular case, given no p5.js library addon is present which would depend on it.
Both async & defer attributes are optional.
Theyāre used to lower the total loading time of the page.
I use a CDN link so I donāt need to have a copy of āp5.min.jsā library on every single folder locally.
Well, never used that. I had no complaints from my text editor.
Like async attribute, defer also loads a <script> in parallel w/ other async & defer scripts.
However, it defers its execution until the whole page is parsed & ready.
Thatās the same default behavior as a ātype=moduleā <script> btW.
But as you know by now, itās totally optional. Itās merely for decreasing page loading time.
Not all: Iāve kept āscriptindex.jsā as ā.jsā, b/c itās the only non-module classic script type, besides the p5.js library.
The files āsketch.mjsā, āAnimal.mjsā and āpoint_coord.mjsā canāt be loaded w/ a regular <script> w/o adding the attribute ātype=moduleā.
The reason is b/c they contain the keywords import and/or export; thus turning them into an ECMA Script Module (ESM) instead:
Although renaming them to ā.mjsā is optional, itās a good practice of letting others know they arenāt classical non-module JS files, that they have different running behaviors.
Also, runtime servers like NodeJS, Deno, Bun, etc., recognize the ā.mjsā extension name and change their import/export behavior accordingly.
The original, and now deprecated, NodeJS module is called CommonJS, and its official extension is ā.cjsā:
Nope! Actually itās very rare to see any Processing devs here in this forum!
This weekend i had to make some kind of document with your teachingā¦
1 - p5.js could use defer or async which is better ? as far as i know we dont have here any promise to use await/asyncā¦
2 - Do you know any way to contact any dev ?
3 - Seems mjs is more node.js, as you can see im developing in browser environmenā¦still need or better said is recommended to use .mjs in modules( i understand modules as any .js with import or export in their code)
4 - Do you have any explanation about the / ā¦ why in most cases workd fine in p5 but in the loading of sketch.js you have to delete it ? perhaps a bug ?
Both attributes async & defer load a <script> w/o waiting for the page to be parsed.
The diff. is that async runs the script right after itās finished loading.
While defer postpones its execution until the page is fully parsed.
If you donāt have other addons which canāt be run before p5.js has run, use defer for all of them; otherwise youāre free to use async for p5.js instead.
Those <script> tag attributes async & defer are used inside an HTML file. Theyāve got nothing to do w/ JS files and its syntax.
All I know is their p5.js repo:
Although itās true itās more useful for Node.JS as a way to differentiate a CommonJS module from an ESM 1, ā.mjsā is ECMAās official extension name for a JS script that acts as a ESM module.
Indeed those 2 keywords make a JS script become an ESM module file.
I dunno all the rules regarding the requirement or not to place a "/" for a filename path.
But for HTML files, I donāt start the path w/ "/" for local files:
Which is preferred or any good practicesā¦put the inside the div or html tag in which the sketch is gonna beā¦ or as you do at the head of the HTML file ? pros and cons ?
Placing a <script> tag inside another tag doesnāt do anything visually!
For a p5.js sketch, what we actually want is its <canvas> element.
In my āsketch.mjsā version, I call method parent() over the p5.Renderer object returned by createCanvas(), passing the <div id="sketch-holder">'s id attribute as its string argument:
This way, the p5.jsās <canvas> becomes a child tag of that <div>.