Successfully created and integrated the @selfx402/pay-widget NPM library into the Selfx402Pay project.
Created a complete, production-ready NPM package:
Structure:
Selfx402PayWidget/
├── dist/ # Built output
│ ├── index.js (48KB) # CommonJS bundle
│ ├── index.mjs (41KB) # ES Module bundle
│ ├── index.d.ts (3.5KB) # TypeScript declarations
│ └── index.d.mts (3.5KB) # ESM TypeScript declarations
├── src/
│ ├── components/
│ │ ├── ui/ # Base UI components
│ │ ├── payment-form.tsx
│ │ ├── payment-form-minimal.tsx
│ │ └── payment-success.tsx
│ ├── lib/utils.ts
│ └── index.ts
├── package.json
├── tsconfig.json
├── README.md
└── .npmignore
Exported Components:
PaymentForm- Full-featured payment formPaymentFormMinimal- Compact form with USDC balancePaymentSuccess- Success screen with animationsButton,Card,Input,Label- UI primitivescn()- Utility function
Build Output:
- ✅ ESM + CJS dual package
- ✅ Full TypeScript type definitions
- ✅ Optimized bundles with tree-shaking support
- ✅ 20KB gzipped package size
Installed the library locally in Selfx402Pay:
# In Selfx402PayWidget/
npm run build
npm pack
# Created: selfx402-pay-widget-0.1.0.tgz
# In Selfx402Pay/
npm install ../Selfx402PayWidget/selfx402-pay-widget-0.1.0.tgzBefore:
import PaymentForm from "@/components/payment-form"
import PaymentFormMinimal from "@/components/payment-form-minimal"After:
import { PaymentForm, PaymentFormMinimal } from "@selfx402/pay-widget"Moved to Backup:
/components/.old-payment-components/payment-form.tsx(archived)/components/.old-payment-components/payment-form-minimal.tsx(archived)/components/.old-payment-components/payment-success.tsx(archived)
✅ Dev Server: Successfully starts on port 3001 ✅ Hot Reload: Works correctly with library imports ✅ Type Safety: Full TypeScript support with autocomplete ✅ No Self-References: Clean separation between app and library
- ✅ No circular dependencies
- ✅ Clear separation of concerns
- ✅ Reusable components across projects
- ✅ Single source of truth for payment components
- ✅ Version control for component library
- ✅ Easy updates across all consuming projects
- ✅ Bug fixes in one place
- ✅ Consistent behavior across implementations
- ✅ Independent testing of library
- ✅ Ready for NPM publication
- ✅ Can be used in any React/Next.js project
- ✅ Includes comprehensive documentation
The app now cleanly imports payment components from the library:
import { PaymentForm, PaymentFormMinimal } from "@selfx402/pay-widget"
export default function Home() {
return (
<Tabs>
<TabsContent value="regular">
<PaymentForm
vendorUrl="http://localhost:3000"
apiEndpoint="/api/demo"
showDeepLink={false}
onPaymentSuccess={(data) => console.log(data)}
onPaymentFailure={(error) => console.error(error)}
/>
</TabsContent>
<TabsContent value="minimal">
<PaymentFormMinimal
vendorUrl="http://localhost:3000"
showDeepLink="both"
/>
</TabsContent>
</Tabs>
)
}- Continue using the library locally for now
- Make updates in
/Selfx402PayWidget/src/ - Rebuild and reinstall when changes are made:
cd Selfx402PayWidget npm run build npm pack cd ../Selfx402Pay npm install ../Selfx402PayWidget/selfx402-pay-widget-0.1.0.tgz
-
Publish to NPM:
cd Selfx402PayWidget npm login npm publish --access public -
Update Selfx402Pay:
cd Selfx402Pay npm uninstall @selfx402/pay-widget npm install @selfx402/pay-widget -
Remove Tarball Installation:
- Update
package.jsonto use"@selfx402/pay-widget": "^0.1.0"instead of file path
- Update
Install the library in any React/Next.js project:
npm install @selfx402/pay-widget
# Peer dependencies (if not already installed)
npm install react react-dom wagmi viem @tanstack/react-query- ✅
app/page.tsx- Updated imports to use library - ✅
components/payment-form.tsx- Moved to.old-payment-components/ - ✅
components/payment-form-minimal.tsx- Moved to.old-payment-components/ - ✅
components/payment-success.tsx- Moved to.old-payment-components/ - ✅
package.json- Added@selfx402/pay-widgetdependency
- ✅ Complete library structure created
- ✅ All components extracted and adapted
- ✅ Build system configured (tsup)
- ✅ TypeScript declarations generated
- ✅ README documentation complete
- ✅ NPM package ready for publication
-
Rebuild the library:
cd Selfx402PayWidget npm run build -
Clear Next.js cache:
cd Selfx402Pay rm -rf .next npm run dev
Reinstall the library:
cd Selfx402Pay
npm uninstall @selfx402/pay-widget
npm install ../Selfx402PayWidget/selfx402-pay-widget-0.1.0.tgz✨ Success! The Selfx402Pay project now uses a clean, professional library architecture with no self-references. The payment components are fully encapsulated in the @selfx402/pay-widget package, ready for reuse across multiple projects and publication to NPM.