Skip to content

A fast, helpful, and open-source document parser

feat(node): add CommonJS exports

remloyalea77b713b6ff
diff --git a/packages/node/README.md b/packages/node/README.md
index 52c9b8c..7f1233f 100644
--- a/packages/node/README.md
+++ b/packages/node/README.md
@@ -26,6 +26,18 @@ for (const page of result.pages) {
}
```
+CommonJS is supported as well:
+
+```javascript
+const { LiteParse } = require('@llamaindex/liteparse');
+
+(async () => {
+ const parser = new LiteParse();
+ const result = await parser.parse('document.pdf');
+ console.log(result.text);
+})();
+```
+
### Bounded-memory parsing
For documents with many text items, consume page batches without retaining
diff --git a/packages/node/package.json b/packages/node/package.json
index 7a28dc6..af140ef 100644
--- a/packages/node/package.json
+++ b/packages/node/package.json
@@ -3,7 +3,8 @@
"version": "2.13.1",
"description": "Fast, lightweight PDF and document parsing with spatial text extraction",
"type": "module",
- "main": "./dist/lib.js",
+ "main": "./dist/lib.cjs",
+ "module": "./dist/lib.js",
"types": "./dist/lib.d.ts",
"bin": {
"lit": "./dist/cli.js",
@@ -12,7 +13,9 @@
"exports": {
".": {
"types": "./dist/lib.d.ts",
- "import": "./dist/lib.js"
+ "import": "./dist/lib.js",
+ "require": "./dist/lib.cjs",
+ "default": "./dist/lib.js"
},
"./package.json": "./package.json"
},
@@ -41,7 +44,7 @@
"scripts": {
"build:rs": "napi build --cargo-cwd ../../crates/liteparse-napi --platform --release --js false --dts native.d.ts .",
"build:pdfium": "bash scripts/copy-pdfium.sh .",
- "build:ts": "tsc",
+ "build:ts": "tsc && tsc -p tsconfig.cjs.json && node scripts/build-cjs.mjs",
"build": "npm run build:rs && npm run build:pdfium && npm run build:ts",
"prepublishOnly": "npm run build"
},
diff --git a/packages/node/scripts/build-cjs.mjs b/packages/node/scripts/build-cjs.mjs
new file mode 100644
index 0000000..f850ac4
--- /dev/null
+++ b/packages/node/scripts/build-cjs.mjs
@@ -0,0 +1,41 @@
+import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
+import { dirname, extname, join, relative, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const packageDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+const inputDir = join(packageDir, ".cjs-build");
+const outputDir = join(packageDir, "dist");
+
+async function emitCommonJsFiles(directory) {
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
+ const inputPath = join(directory, entry.name);
+ if (entry.isDirectory()) {
+ await emitCommonJsFiles(inputPath);
+ continue;
+ }
+
+ const extension = extname(entry.name);
+ if (extension !== ".js" && extension !== ".map") continue;
+
+ const relativePath = relative(inputDir, inputPath);
+ const outputPath = join(
+ outputDir,
+ relativePath.replace(/\.js(\.map)?$/, ".cjs$1"),
+ );
+ let contents = await readFile(inputPath, "utf8");
+ contents = contents.replace(
+ /require\((['"])(\.\.?\/[^'"]+)\.js\1\)/g,
+ "require($1$2.cjs$1)",
+ );
+ contents = contents.replace(/\.js\.map/g, ".cjs.map");
+
+ await mkdir(dirname(outputPath), { recursive: true });
+ await writeFile(outputPath, contents);
+ }
+}
+
+try {
+ await emitCommonJsFiles(inputDir);
+} finally {
+ await rm(inputDir, { recursive: true, force: true });
+}
diff --git a/packages/node/src/native.ts b/packages/node/src/native.ts
index d247d6d..50b233b 100644
--- a/packages/node/src/native.ts
+++ b/packages/node/src/native.ts
@@ -4,11 +4,37 @@
// provides the .node binary. During development: `napi build` places it alongside package.json.
import { createRequire } from "node:module";
-import { join, dirname } from "node:path";
-import { fileURLToPath } from "node:url";
+import { join, dirname, resolve } from "node:path";
-const require = createRequire(import.meta.url);
-const __dirname = dirname(fileURLToPath(import.meta.url));
+declare const require: NodeRequire | undefined;
+declare const __dirname: string | undefined;
+
+function getRuntimeContext(): { require: NodeRequire; dirname: string } {
+ if (typeof require === "function" && typeof __dirname === "string") {
+ return { require, dirname: __dirname };
+ }
+
+ const entryPoint = process.argv[1]
+ ? resolve(process.argv[1])
+ : join(process.cwd(), "package.json");
+ const entryRequire = createRequire(entryPoint);
+ let packageDir = process.cwd();
+
+ try {
+ packageDir = dirname(
+ entryRequire.resolve("@llamaindex/liteparse/package.json"),
+ );
+ } catch {
+ // Development builds may not be installed as a package yet.
+ }
+
+ return {
+ require: createRequire(join(packageDir, "package.json")),
+ dirname: packageDir,
+ };
+}
+
+const runtime = getRuntimeContext();
interface NativeBindings {
LiteParse: new (config?: LiteParseNativeConfig) => LiteParseNative;
@@ -404,7 +430,7 @@ function loadNative(): NativeBindings {
const pkg = triples[key];
if (pkg) {
try {
- return require(pkg);
+ return runtime.require(pkg);
} catch {
// Not installed, try next
}
@@ -413,7 +439,11 @@ function loadNative(): NativeBindings {
// Fallback: local .node file (development builds)
// Try several paths since __dirname may be dist/ or dist/src/
- const searchDirs = [__dirname, join(__dirname, ".."), join(__dirname, "..", "..")];
+ const searchDirs = [
+ runtime.dirname,
+ join(runtime.dirname, ".."),
+ join(runtime.dirname, "..", ".."),
+ ];
// Try full triple names (e.g. liteparse.linux-x64-gnu.node) and simple name
const fileNames = [
...candidates.map((c) => `liteparse.${c}.node`),
@@ -423,7 +453,7 @@ function loadNative(): NativeBindings {
for (const dir of searchDirs) {
for (const fileName of fileNames) {
try {
- return require(join(dir, fileName));
+ return runtime.require(join(dir, fileName));
} catch {
// try next
}
diff --git a/packages/node/tsconfig.cjs.json b/packages/node/tsconfig.cjs.json
new file mode 100644
index 0000000..3f3e60c
--- /dev/null
+++ b/packages/node/tsconfig.cjs.json
@@ -0,0 +1,11 @@
+{
+ "extends": "./tsconfig.json",
+ "compilerOptions": {
+ "module": "CommonJS",
+ "moduleResolution": "Node",
+ "outDir": "./.cjs-build",
+ "declaration": false,
+ "declarationMap": false
+ },
+ "exclude": ["node_modules", "dist", ".cjs-build"]
+}