Skip to content

Commit 035b126

Browse files
committed
use a workaround
1 parent 5f7509b commit 035b126

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/api/constants.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@
1313
// limitations under the License.
1414

1515
import { LIB_NAME, LIB_VERSION } from '@/src/version';
16+
import * as path from "path";
17+
import * as fs from "fs";
1618

1719
export const RAGSTACK_REQUESTED_WITH = (() => {
20+
/**
21+
* Do not use require() here, it will break the build in some environments such as NextJS application
22+
* if @datastax/ragstack-ai is not installed (which is perfectly fine).
23+
*/
1824
try {
19-
// eslint-disable-next-line @typescript-eslint/no-var-requires
20-
const ragstack = require('@datastax/ragstack-ai');
21-
const version = ragstack['RAGSTACK_VERSION'] || "?";
22-
return `ragstack-ai-ts/${version}`
25+
const expectedFilePath = path.join('@datastax', 'ragstack-ai', 'package.json');
26+
for (const nodeModulesPath of module.paths) {
27+
const fullFilePath = path.join(nodeModulesPath, expectedFilePath);
28+
if (fs.existsSync(fullFilePath)) {
29+
const packageJson = JSON.parse(fs.readFileSync(fullFilePath, 'utf8'))
30+
return `ragstack-ai-ts/${packageJson.version}`
31+
}
32+
}
33+
return ''
2334
} catch (e) {
2435
return '';
2536
}

0 commit comments

Comments
 (0)