Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work on latest Node #59

Open
aMyTimed opened this issue Mar 24, 2023 · 6 comments
Open

Doesn't work on latest Node #59

aMyTimed opened this issue Mar 24, 2023 · 6 comments

Comments

@aMyTimed
Copy link

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
Copy link

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

@8Observer8
Copy link

8Observer8 commented Jul 14, 2023

I had the same problem but @Birch-san helped me here #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
Copy link

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

@8Observer8
Copy link

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
Copy link

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

@aMyTimed
Copy link
Author

alright, ill add this to my fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants