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
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>.