fix(cli): clearer error when importing .node addon via ESM#34361
Conversation
Importing a native `.node` addon through an ESM `import` statement previously surfaced as `SyntaxError: Unexpected character '\xff'` because the binary file was handed to V8 as JavaScript. Native addons in Node.js can only be loaded through `require()`, so detect the `.node` extension in the module loader and return a TypeError that points users at `createRequire()` from `node:module`. Fixes #17246
fibibot
left a comment
There was a problem hiding this comment.
No code issue found. The early .node guard in load_inner() runs after resolution and before load_code_source(), so ESM imports get the clearer TypeError while CommonJS require() continues through Module._extensions[".node"]; the spec locks the node_modules subpath case. Holding approval until CI is green.
fibibot
left a comment
There was a problem hiding this comment.
CI found a real lint failure on this revision, so this needs one small Rust cleanup before approval.
JsErrorBox::type_error(...).into()incli/module_loader.rstripsclippy::useless_conversionbecause the expression is already aJsErrorBox. Remove the.into()and return theJsErrorBoxdirectly fromErr(...).
fibibot
left a comment
There was a problem hiding this comment.
The new head removes the .into() that failed clippy, so the prior lint blocker is gone. The .node guard still runs after resolution and before load_code_source(), which gives ESM imports the typed error without touching CommonJS Module._extensions[".node"]; the node_modules spec covers that path. Holding approval until CI is green.
fibibot
left a comment
There was a problem hiding this comment.
CI is green now, promoting prior review to APPROVE.
…34361) Importing a native `.node` addon through an ESM `import` statement previously surfaced as `SyntaxError: Unexpected character '\xff'` because the binary file was handed to V8 as JavaScript. Native addons in Node.js can only be loaded through `require()`, so detect the `.node` extension at the start of `load_inner` and return a `TypeError` that points users at `createRequire()` from `node:module`. The legitimate CJS paths (`import "npm:some-pkg"` where the package internally `require`s a `.node` file, and explicit `createRequire()` + `require("./addon.node")` in user code) are unaffected. Fixes denoland#17246
Importing a native
.nodeaddon through an ESMimportstatementpreviously surfaced as
SyntaxError: Unexpected character '\xff'becausethe binary file was handed to V8 as JavaScript. Native addons in Node.js
can only be loaded through
require(), so detect the.nodeextensionat the start of
load_innerand return aTypeErrorthat points usersat
createRequire()fromnode:module. The legitimate CJS paths(
import "npm:some-pkg"where the package internallyrequires a.nodefile, and explicitcreateRequire()+require("./addon.node")in user code) are unaffected.
Fixes #17246