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: