Skip to content

Commit 6ba6c89

Browse files
committed
fix: use plain require for native binding in CJS context
1 parent 74637f2 commit 6ba6c89

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opencode-codebase-index",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Semantic codebase indexing and search for OpenCode - find code by meaning, not just keywords",
55
"type": "module",
66
"main": "dist/index.cjs",

src/native/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@ function getNativeBinding() {
2323
throw new Error(`Unsupported platform: ${platform}-${arch}`);
2424
}
2525

26-
// Get the directory of this module - works in both ESM and bundled contexts
27-
const currentFileUrl = typeof import.meta !== 'undefined' ? import.meta.url : __filename;
28-
const currentDir = typeof currentFileUrl === 'string' && currentFileUrl.startsWith('file:')
29-
? path.dirname(fileURLToPath(currentFileUrl))
30-
: __dirname;
26+
// Determine the current directory - handle ESM, CJS, and bundled contexts
27+
let currentDir: string;
28+
29+
// Check for ESM context with valid import.meta.url
30+
if (typeof import.meta !== 'undefined' && import.meta.url) {
31+
currentDir = path.dirname(fileURLToPath(import.meta.url));
32+
}
33+
// Fallback to __dirname for CJS/bundled contexts
34+
else if (typeof __dirname !== 'undefined') {
35+
currentDir = __dirname;
36+
}
37+
// Last resort: use process.cwd() - shouldn't normally hit this
38+
else {
39+
currentDir = process.cwd();
40+
}
3141

3242
// The native module is in the 'native' folder at package root
3343
// From dist/index.js, we go up one level to package root, then into native/
@@ -38,8 +48,8 @@ function getNativeBinding() {
3848
: path.resolve(currentDir, '..');
3949
const nativePath = path.join(packageRoot, 'native', bindingName);
4050

41-
// Use createRequire to load .node files in ESM context
42-
const require = createRequire(currentFileUrl);
51+
// Load the native module - use standard require for .node files
52+
// eslint-disable-next-line @typescript-eslint/no-require-imports
4353
return require(nativePath);
4454
}
4555

0 commit comments

Comments
 (0)