Adding node module to p5.js

Hi everyone. I’m working on bin packing project. I want to use node module to placement of rectangles.

Node module that I want to use: rectangle-bin-pack - npm

I’m using p5.vscode extension on VS code for develop. Is there any tutorial or document to about adding node modules? Thanks already for your help. Have a nice day.

Hi! welcome to the forum!

In short, it’s not possible in this case (or at least it’s not easy).

Long answer - unfortunately this package uses c++, so it only runs on node.js and not on client/browser - maybe you can use webassembly but you need to port the code and compile it by yourself, I guess. If the npm module is built only by javascript, it may be possible to run on the client side. Just like you include p5.js in an html file, you can include the module as standalone js file (for example, like adding ml5.js) which will be exposed as global variables. Another way, which is more “elegant”, is to use tools like browserify to bundle all the javascript file including modules (p5.js etc) into a single js file and include it in the html file. The advantage is that you can keep track of dependencies of npm modules by package.json (but again, this does not support every npm module, and the one you mentioned cannot be included because of the reason above), but the downside is that you need to set up your environment for development.


Thank you for answer, I think I should find another node module to packing. After that I will try to adding standalone or with Browserify.