Skip to content

Commit 2d17eb6

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.
1 parent c64891f commit 2d17eb6

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

packages/react-native-node-api-modules/src/node/path-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ const packageNameCache = new Map<string, string>();
3131
* TODO: Consider checking for a specific platform extension.
3232
*/
3333
export function isNodeApiModule(modulePath: string): boolean {
34-
// HACK: Take a shortcut (if applicable)
35-
if (modulePath.endsWith('.node')) {
34+
{
35+
// HACK: Take a shortcut (if applicable): existing `.node` files are addons
3636
try {
37-
fs.accessSync(modulePath);
37+
fs.accessSync(modulePath.endsWith(".node") ? modulePath : `${modulePath}.node`);
3838
return true;
3939
} catch {}
4040
}

0 commit comments

Comments
 (0)