Cannot import from a Node.js ESM package
relrelb opened this issue · comments
relrelb commented
Consider the following simple package:
package.json
{
"type": "module",
"dependencies": {
"wasm-feature-detect": "^1.5.0"
}
}
index.js
import { simd } from "wasm-feature-detect";
console.log(simd);
Running the following:
npm install
node index.js
Should've output:
[AsyncFunction: simd]
But instead the following error is produced:
file:///.../index.js:1
import { simd } from "wasm-feature-detect";
^^^^
SyntaxError: Named export 'simd' not found. The requested module 'wasm-feature-detect' 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 'wasm-feature-detect';
const { simd } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
Node.js v19.6.1
I suspect the problem resides in the following line:
wasm-feature-detect/package.json
Line 6 in d5b2de2
According to https://nodejs.org/api/packages.html#nodejs-packagejson-field-definitions, the "module"
field doesn't even exist, but "exports"
does. Applying the following patch fixes the problem:
- "module": "dist/esm/index.js",
+ "exports": "./dist/esm/index.js",
Surma commented
Oh great catch. Thank you. I think the module
field was never standard, but supported by bundlers. Anyway, whipping up a PR now