Birch-san / box2d-wasm

Box2D physics engine compiled to WebAssembly. Supports TypeScript and ES modules.
263 stars 21 forks source link

Doesn't work on latest Node #59

Open aMyTimed opened 1 year ago

aMyTimed commented 1 year ago

it works with v14.8.0 with nvm, but with v18.5.0 or v19 (haven't tried others) it spits this out:

./node_modules/box2d-wasm/dist/umd/Box2D.simd.js:12

TypeError: Failed to parse URL from ./node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm
    at Object.fetch (node:internal/deps/undici/undici:11413:11) {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
ben-zabloski commented 1 year ago

It appears to work with node v17.9.1 as well.

8Observer8 commented 11 months ago

I had the same problem but @Birch-san helped me here https://github.com/Birch-san/box2d-wasm/issues/65

Solution:

init-box2d.js

import Box2DLib from "box2d-wasm";

export let box2d = null;

export function initBox2D() {
    return new Promise(resolve => {
        Box2DLib({
            locateFile: (url, scriptDirectory) => "file:///opt/render/project/src/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm"
        }).then((re) => {
            box2d = re;
            resolve();
        });
    });
}

app.js

import { box2d, initBox2D } from "./init-box2d.js";

async function init() {
    await initBox2D();

    const {
        b2Vec2,
        b2World
    } = box2d;

    const gravity = new b2Vec2(0, -3);
    const world = new b2World(gravity);
    const g = world.GetGravity();
    console.log("gravity = ", g);
}

init();
ben-zabloski commented 11 months ago

Looks good - I'll have to try it out! 😊

8Observer8 commented 11 months ago

Small update for localhost:

import Box2DLib from "box2d-wasm";

export let box2d = null;

export function initBox2D(localhost = true) {
    return new Promise(resolve => {
        Box2DLib({
            locateFile: (url, scriptDirectory) => {
                if (url.endsWith(".wasm") && !localhost) {
                    return "file:///opt/render/project/src/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm";
                }
                return scriptDirectory + url;
            }
        }).then((re) => {
            box2d = re;
            resolve();
        });
    });
}
8Observer8 commented 11 months ago

But my example is for deploying on Render.com hosting. You should change the path above to yours. I hope it will help.

aMyTimed commented 11 months ago

alright, ill add this to my fork

CompositionCloud commented 1 week ago

Hello,

I'm experiencing a similar issue with Node v20.15.0.

I initially got this error:

TypeError: Failed to parse URL from /Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm
/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.js:12
q){e?d(e):c(q.buffer)})},1<process.argv.length&&process.argv[1].replace(/\\/g,"/"),process.argv.slice(2),process.on("uncaughtException",function(b){throw b;}),process.on("unhandledRejection",oa),a.inspect=function(){return"[Emscripten Module object]"};else if(ea||ha)ha?ia=self.location.href:"undefined"!==typeof document&&document.currentScript&&(ia=document.currentScript.src),_scriptDir&&(ia=_scriptDir),ia=0!==ia.indexOf("blob:")?ia.substr(0,ia.lastIndexOf("/")+1):"",ja=function(b){var c=new XMLHttpRequest;
                                                                                                                                                    ^

RuntimeError: abort(TypeError: Failed to parse URL from /Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm). Build with -s ASSERTIONS=1 for more info.
    at process.oa (/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.js:16:62)
    at process.emit (node:events:519:28)
    at emitUnhandledRejection (node:internal/process/promises:250:13)
    at throwUnhandledRejectionsMode (node:internal/process/promises:385:19)
    at processPromiseRejections (node:internal/process/promises:470:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)

I tried your solution, @8Observer8, but then I got this error:

Error: ENOENT: no such file or directory, open 'file:/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm'
failed to asynchronously prepare wasm: RuntimeError: abort(Error: ENOENT: no such file or directory, open 'file:/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm'). Build with -s ASSERTIONS=1 for more info.
RuntimeError: abort(Error: ENOENT: no such file or directory, open 'file:/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm'). Build with -s ASSERTIONS=1 for more info.
RuntimeError: abort(Error: ENOENT: no such file or directory, open 'file:/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm'). Build with -s ASSERTIONS=1 for more info.
/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.js:12
q){e?d(e):c(q.buffer)})},1<process.argv.length&&process.argv[1].replace(/\\/g,"/"),process.argv.slice(2),process.on("uncaughtException",function(b){throw b;}),process.on("unhandledRejection",oa),a.inspect=function(){return"[Emscripten Module object]"};else if(ea||ha)ha?ia=self.location.href:"undefined"!==typeof document&&document.currentScript&&(ia=document.currentScript.src),_scriptDir&&(ia=_scriptDir),ia=0!==ia.indexOf("blob:")?ia.substr(0,ia.lastIndexOf("/")+1):"",ja=function(b){var c=new XMLHttpRequest;
                                                                                                                                                    ^

RuntimeError: abort(RuntimeError: abort(Error: ENOENT: no such file or directory, open 'file:/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.wasm'). Build with -s ASSERTIONS=1 for more info.). Build with -s ASSERTIONS=1 for more info.
    at process.oa (/Users/gonipeles/Desktop/phd/scorecraft/node_modules/box2d-wasm/dist/umd/Box2D.simd.js:16:62)
    at process.emit (node:events:519:28)
    at emitUnhandledRejection (node:internal/process/promises:250:13)
    at throwUnhandledRejectionsMode (node:internal/process/promises:385:19)
    at processPromiseRejections (node:internal/process/promises:470:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)

I can confirm that this file does exist...

I'd appreciate any help... Thanks!