Lusito / box2d.ts

Full blown Box2D Ecosystem for the web, written in TypeScript
https://lusito.github.io/box2d.ts
60 stars 6 forks source link

ReferenceError: performance is not defined #4

Open DanielHZhang opened 3 years ago

DanielHZhang commented 3 years ago

This seems to be originating from the b2Timer class and its usage of performance.now() when @box2d/core is executed from nodejs. A simple fix would be to synchronously require the performance module when running in node>=8.5.

export const performance =
    typeof globalThis.window !== "undefined"
        ? window.performance
        : (require("perf_hooks") as typeof import("perf_hooks").performance;

However, this leads to Module "perf_hooks" not found error when bundling with webpack, unless {externals: {perf_hooks: 'empty'}} is set in the config.

Lusito commented 3 years ago

Check out https://github.com/Lusito/box2d.ts/blob/master/packages/benchmark/src/cli/index.ts to see how I fix it in the benchmarks.

Out of curiousity: Are you using box2d in node.js other than for benchmarking?

DanielHZhang commented 3 years ago

I'm using box2d server-side as part of my authoritative game server architecture. Clients run box2d locally in their browser and send their inputs to the nodejs server. The server is also running box2d in a worker thread, which computes the results of received inputs, then sends the updated world state back to each client.

dongshimou commented 8 months ago

same problem when i use node v14.x. solve this problem with node v16.x.