Skip to content

Commit bb8b7b3

Browse files
author
Mariusz Pasinski
committed
feat: relax the condition for CJS modules without file exts
This small change relaxes the condition for taking the shortcut, as CommonJS modules (in contrast to ESM) do not require developers to explicitly include the file extensions. The Node.js module resolution algorithm (https://nodejs.org/api/modules.html#all-together) in step 4 of LOAD_AS_FILE(X) would try appending the `.node` extension. In theory, we should make sure that other extensions checked in previous steps are not present, but given that we are implementing it for `requireNodeAddon()`, it should be safe to skip those. # Conflicts: # packages/host/src/node/path-utils.ts
1 parent 75ac1fa commit bb8b7b3

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

packages/host/src/node/path-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const packageNameCache = new Map<string, string>();
3131
* TODO: Consider checking for a specific platform extension.
3232
*/
3333
export function isNodeApiModule(modulePath: string): boolean {
34+
{
35+
// HACK: Take a shortcut (if applicable): existing `.node` files are addons
36+
try {
37+
fs.accessSync(modulePath.endsWith(".node") ? modulePath : `${modulePath}.node`);
38+
return true;
39+
} catch {}
40+
}
3441
const dir = path.dirname(modulePath);
3542
const baseName = path.basename(modulePath, ".node");
3643
let entries: string[];

0 commit comments

Comments
 (0)