You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description import {WebMidi} from "webmidi" is not working in node, despite what the official documentation says. I get an error about Named export 'WebMidi' not found.
To reproduce, simply create a new directory
npm i webmidi
add "type": "module", to the package.json
create a file main.js with
import{WebMidi}from"webmidi";
run with node main.js and I get
$ node main.js
file:///private/tmp/wwww/main.js:1
import {WebMidi} from "webmidi";
^^^^^^^
SyntaxError: Named export 'WebMidi' not found. The requested module 'webmidi' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'webmidi';
const {WebMidi} = pkg;
Also tried using node v16.14.2 on a different machine, an M1 mac - same problem.
Also tried in a Ubuntu 22.04 VM using node v12.22.9 - same problem.
Renaming from main.js to main.mjs does not change the error.
Details
Add any other information, context or details that could help track down the problem.
Trying an alternative import syntax
import*asWebMidifrom"webmidi"
instead changes the error to:
$ node main.js
(node:53862) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/private/tmp/wwww/node_modules/webmidi/dist/esm/webmidi.esm.min.js:395
*/const u=new class extends ................. WebMidi};
SyntaxError: Unexpected token 'export'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
Taking node's advice and changing the import to
importpkgfrom'webmidi';const{ WebMidi }=pkg;
fails with the same Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.... SyntaxError: Unexpected token 'export' error.
Discussion
Interestingly, the simple module example given in #252
You are very right. As I explain here, PR #254 introduced a regression problem that prevented Node.js from correctly identifying the type of module to load (ESM or CommonJS)
The problem has been fixed in release 3.0.20 and is now available on NPM.
This highlights the need for plaftorm-targeted unit tests going forward. Thanks a lot for reporting.
Description
import {WebMidi} from "webmidi"
is not working in node, despite what the official documentation says. I get an error aboutNamed export 'WebMidi' not found
.To reproduce, simply create a new directory
npm i webmidi
"type": "module",
to thepackage.json
main.js
withrun with
node main.js
and I getmy package.json is
Environment:
v16.13.1
Seems also to be related to issues #89 and #82
v16.14.2
on a different machine, an M1 mac - same problem.v12.22.9
- same problem.main.mjs
does not change the error.Details
Add any other information, context or details that could help track down the problem.
Trying an alternative import syntax
instead changes the error to:
Taking node's advice and changing the import to
fails with the same
Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.... SyntaxError: Unexpected token 'export'
error.Discussion
Interestingly, the simple module example given in #252
supposedly works for @djipco - but I can't get past the import problem mentioned above.
The text was updated successfully, but these errors were encountered: