djipco / webmidi

Tame the Web MIDI API. Send and receive MIDI messages with ease. Control instruments with user-friendly functions (playNote, sendPitchBend, etc.). React to MIDI input with simple event listeners (noteon, pitchbend, controlchange, etc.).
Apache License 2.0
1.54k stars 115 forks source link

Cannot run WebMidi on recently released Node v21.1.0 #392

Closed nnirror closed 10 months ago

nnirror commented 10 months ago

Description

A user of a repository I maintain which includes the WebMIDI npm package reported this issue, which I have been able to duplicate locally.

I cannot run WebMIDI on Node v21.1.0, which was released several weeks ago. I did test that the issue does not occur running Node v18.14.0, nor does it occur running v14.20.0.

So I suspect it must be specific to the very recently released node versions.

If I run the following test.js script:

const {WebMidi} = require('webmidi');

I get the following error:

TypeError: Cannot set property navigator of #<Object> which has only a getter
    at Object.<anonymous> (/Users/cella/Desktop/test/node_modules/webmidi/dist/cjs/webmidi.cjs.min.js:445:4390)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/Users/cella/Desktop/test/test.js:1:19)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)

Environment: Specify the environment where you are witnessing the problem:

Details Thank you for your time! WebMIDI is amazing.

djipco commented 10 months ago

Version 21.0.0 of Node.js has added its own (experimental) global.navigator object which is meant to be a partial implementation of window.navigator. This breaks WEBMIDI.js support.

For now, you can run Node version 21.0.0 with the --no-experimental-global-navigator flag to work around the issue:

node --no-experimental-global-navigator test.js

I will have to look at an option that does not break compatibility. I'll keep you posted.

djipco commented 10 months ago

Version 3.1.7, which is available right now, fixes this problem. If you want to use Node.js v21+, you need to update.