Three with Node and Server Side Three

Hobbyist web programmer here that just built a fun three.js app that does some engineering calculations for me. I mostly use three.js for the user interface and graphics.

I now want to expose this code externally to other computers with what I think is this mythical “API” term, eliminating the need to interact with the three.js user interface.

In order to do this, I think I need to move the code server-side and have users send a json type input and have the server respond with a json file type output of engineering results.

From what I have read, there might be some issues with running three.js server-side.
The api calls would rely only very minimally on three. For example, I store shapes for the engineering calculations in the three.js classes, for example:

createShapeFromPoints(points) {
        const shape = new THREE.Shape();
        if (points.length === 0) return shape;
        
        // Move to first point
        const firstPoint = new THREE.Vector2(points[0].x, points[0].y);
        shape.moveTo(firstPoint.x, firstPoint.y);
        
        // Connect remaining points with lines
        for (let i = 1; i < points.length; i++) {
            shape.lineTo(points[i].x, points[i].y);
        }
        
        // Close the shape
        shape.lineTo(firstPoint.x, firstPoint.y);
        
        // Add holes if provided
        this.holes.forEach(hole => shape.holes.push(hole));
        
        return shape;
    }

In addition, there are nice functions that I use three.js like computeBoundingSphere() in some locations throughout the code.

At no point in the api response would I need to call things like canvas elements that exist in the web api only.

So, with that, would I be better recreating all of the three.js functions in vanilla javascript, or would it be ok to utilize minimal three.js code in the server side api calls?

Thanks!

If you’re not using renderers, materials, or textures, then I think you’ll find the remainder of the three.js library works fine in a Node.js server environment. But I would just start with something very simple like…

import * as THREE from 'three';

const a = new THREE.Vector2(0, 0);

console.log(a);

… to confirm that there’s nothing more tricky with your Node.js version, CommonJS vs. ESM modules, or something like that.