Skip to content

fix: prevent native modules from being bundled for client-side code#189

Open
sh1ftred wants to merge 1 commit into
sdk-integrationfrom
fix/native-module-bundling
Open

fix: prevent native modules from being bundled for client-side code#189
sh1ftred wants to merge 1 commit into
sdk-integrationfrom
fix/native-module-bundling

Conversation

@sh1ftred
Copy link
Copy Markdown
Contributor

Summary

This PR fixes the Module not found error that occurred when running npm run dev, caused by better-sqlite3 being bundled for client-side code.

Changes

Root Cause

better-sqlite3 is a native Node.js module that requires fs, which does not exist in browser environments.

Fixes Applied

  1. next.config.ts

    • Added better-sqlite3 to serverExternalPackages to exclude from bundling
    • Added webpack null-loader rules for native Node.js modules (fs, path, crypto)
    • Added fallback configuration for Node.js modules
  2. sdk/storage/drivers/sqlite.ts

    • Changed require("better-sqlite3") to dynamic await import("better-sqlite3")
    • Added lazy initialization pattern for async database loading
    • Added webpackIgnore comment for bun:sqlite import
  3. sdk/storage/usageTracking/sqlite.ts

    • Applied the same dynamic import pattern
  4. Type System Updates

    • Added "lazyrefund" to RoutstrClientMode type
    • Added "lazyrefund" to RouteRequestOptions.mode
  5. Script Updates

    • Updated check-balance.ts, refund-all.ts, set-client-ids.ts to fix type compatibility issues
    • Updated to use new storage API (getCachedReceiveTokens instead of getCachedTokenDistribution)

Testing

  • npm run build succeeds
  • npm run dev starts successfully without errors

- Add better-sqlite3 to serverExternalPackages in next.config.ts
- Convert sqlite drivers to use dynamic imports with lazy initialization
- Add webpack null-loader rules for native Node.js modules (fs, path, crypto)
- Add webpackIgnore comment for bun:sqlite import
- Add lazyrefund mode to RoutstrClientMode type
- Update scripts to use new storage API (getCachedReceiveTokens)
- Fix type compatibility issues in scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant