I spent countless hours to get my editor (VSC) to autocomplete P5JS.
This worked only after adding this line to the main script:
import "./p5"
Now everything is accessible only if prefixed by p5, and the editor autocompletion and online documentation finally work.
Also, I’ve read that this importing method is not recommended for some reason, but it’s the only way to get a reasonable coding environment, so I guess I should change it? But then, how do I get the documentation and autocompletion to work?
Since you are on VSCode, have you seen this p5js extension?
It says:
p5.vscode helps you create p5.js projects in Visual Studio Code. It also includes autocompletion, a simple tool to browse and install third-party p5 libraries, and the Live Server extension.
I did install the p5.vscode extension, and things are much better. I.e. documentation and autocompletion do work for P5JS.
In my current project I’m also using https://unpkg.com/ml-matrix@6.8.0/matrix.umd.js. In order to get Intellisense to work, I believe I need to use some sort of
/// <reference path="…"
syntax, where I’m passing a TS file. Now, I have no idea where to get that.
I’m extremely new to JS and extremely lost.
Edit
Okay, the file lives here https://unpkg.com/ml-matrix@6.8.0/matrix.d.ts. But nothing works. The frustration grows, LOL.
If you are using multiple libraries in your project, it’s strongly recommended to use a package manager like NPM (which is a standard in web development).
A quick example of npm (assuming you are on Linux):
$ mkdir p5js-test # creates a new project folder
$ cd p5js-test
$ npm init -y # creates a new project using npm, -y yes to all
$ npm install ml-matrix # install package
$ code . # open VSCode
With that I have instant autocompletion in VSCode because all the packages are located inside a node_modules folder.
Isn’t this used for server-side code?
I’m really a newbie…
I’m putting together this demo, which is using a numerical external library, which is not going to be installed on the client.
I would like to be able to access to autocompletion and documentation within the editor (like I’m used to when I’m coding anything in Python).
Thank you again for helping out.
Edit
I’ve downloaded https://unpkg.com/ml-matrix@6.8.0/matrix.d.ts in side the libraries folder and added the following line to the beginning of my script
const mlMatrix = require("./libraries/matrix");
Now VSC autocompletion and documentation work.
I only need to comment out the line before saving, so it does not break the execution.
I’m back using the online interface (without autocompletion or documentation), at least I don’t need to fight VSC. The only thing that’s scary is that I cannot use Git in the online editor.
Edit 2
I’ll try again tomorrow. Today I was too irrational to get it to work.
Yes you’re mostly right. NPM installs the packages in a node_modules folder and therefore not adapted to use within a <script> tag in an HTML page. You need to use a bundler (webpack, browserify, grunt…) that packs all the code into js, css and html files.
This guide explains it very well:
But if it’s a small project, don’t bother using npm and webpack for example since it’s quite a setup.
Of course, if I try to save with line 1 uncommented, then it does not run.
[image|690x286](upload://yVVvFgz34iWX5bgEVKgiyJPNJ4c.png Alt-text: script.js:1 Uncaught SyntaxError: Cannot use import statement outside a module.
So, it seems like I need to keep toggling back and forth line 1. This seems a little crazy.
Am I doing anything wrong? Shall I post this question on, say, StackOverflow, since it’s not Processing related?
P.S. Only one image is allowed for new users… If a moderator can add a ! in front we can all see the pictures. Also, up to two links. Jeez… Plz, add two missing )…
I did that, but then it works only if I don’t prepend mlMatrix. to all instructions.
I think it’s a lost cause.
When importing via <script src> the package add itself under the name mlMatrix, but the IDE has no clue about this.
Adding import * as mlMatrix from "ml-matrix" makes it work, but breaks the code, if not commented out.
I’ll create an issue on Stack Overflow, since this is not related to Processing.
Thank you again for your time.
I’ll post here the solution if there is any.
The file becomes a JS module and it needs a <script type=module> to load inside the HTML.
JS checks the import line if a file w/ that exactly path & name exists so it can load it as a module.
As we can note the file “ml-matrix” doesn’t exist.
Instead there are “matrix.js” & “matrix.umd.js”.
However neither is a JS module but just vanilla JS code.
The library’s types is called “matrix.d.ts”.
But to make things worse “matrix.umd.js” is exposed as namespace mlMatrix, while “matrix.d.ts” exposes its main class as Matrix.
It doesn’t seem very feasible to have at the same time types when coding & auto deployment w/o the trouble of manually commenting & uncommenting the line w/ import.
Unless you code some script which automatically do that toggling magic for you.
Anyways, I’ve found a workaround for it.
However you’re gonna need to switch to TypeScript in order to pull that out.
TypeScript automatically skips any import statements when transpiling our code to JavaScript as long as we just use those imports as datatypes and never as the actual thing.
The main workaround are these few lines below:
import * as p5 from "p5";
import {} from "p5/global";
import * as mlmtx from "ml-matrix";
declare var mlMatrix: typeof mlmtx;
After those lines we can freely use mlMatrix as a namespace for the whole “ML-Matrix” library.
And none of those lines will show up at the transpiled JS file!
Breaking links because new users cannot post links… Moderator, plz, edit my posts (this and my previous one).
Hi @GoToLoo, thank you very much for your extensive reply. Let me see if I understand what you’re saying (I think I am still missing a few bits of info).
Still
So, how is that ml-matrix does not exist, yet it fixes autocompletion and documentation?
So, there are three major files (how do we know this? I see many files in this repo).
How do we learn this? I tried reading that file, but it’s machine generated.
This implies that I will have to…?
I’m not entirely sure what using TypeScript implies.
But I understand that it’s a workaround to get the IDE to read the documentation.
So, if I understand correctly, you created the file sketch.ts, which gets transpiled into sketch.js.
The latter is using P5JS as a module, and uses globalThis.setup and globalThis.draw.
Why not simply using P5JS as a module to begin with? Why is this transpilation necessary?
How is everyone else coding anything in JavaScript? It seems like rather hacky.
I don’t see <HTML>, <HEAD>, nor <BODY>. How is this a legal page?
Also, you include ml-matrix and not matrix.umd.js or matrix.d.ts. How is this working?
JavaScript has had modules for a long time. However, they were implemented via libraries, not built into the language. ES6 is the first time that JavaScript has built-in modules.
That’s a workaround to place both setup() & draw() callbacks in the global context so the library “p5.js” can find & invoke them.
The reason why something like function setup() {} won’t work is b/c file “sketch.js” is a JS module.
JS modules got their own global context separated from the browser’s global context!
Formally that’s an illegal HTML file. However browsers have always been very lenient about rules.
But if you care to inspect the generated page of that “Index.html” file you’ll find those missing elements are automatically inserted.
The reason I code such minimum runnable HTML files is for easier reading w/o unnecessary distractions of having so many <tags> all over the place.
Obviously when the time comes to deploy any project for the final users you’d want a legal HTML file.
“matrix.d.ts” is the TypeScript definition file for “matrix.js”:
“matrix.d.ts” isn’t a runnable file but merely describes the type definitions for a JS file.
TypeScript is a JS superset language which has to be transpiled to JS in order to run:
As you’ve already noticed, if you keep using JS for your current project, you’ll have to comment out back & forth the import statement so the code is valid to run in a browser.
On the other hand we need a “tsconfig.json” for calling “tsc” to compile the TS files to JS:
In my Atom IDE I have an addon which auto-compiles when I save a TS file.
But I bet Visual Studio should have some addon for that as well.
Although TS has its fill of advanced stuff, coding in it basically involves adding a colon : followed by a datatype when the IDE can’t auto-infer it:
Above parameter matrix was declared as datatype mlmtx.Matrix.
Alternatively we can just say its datatype is any if we’re feeling lazy: function displayMatrix(matrix: any) {