The `creditcards` package provides a suite of utility methods for parsing, formatting, and validating credit card numbers, CVCs, and expiration dates. Currently at version 5.0.0, it offers a robust solution for handling common payment-related data operations in applications. While no explicit release cadence is stated, the package is actively maintained and ships with TypeScript types, promoting type safety and improved developer experience. A key differentiator is its modular design, allowing developers to import specific functionalities like `card` or `expiration` individually, and the ability to inject custom card types via the `withTypes` function or by importing individual modules from `creditcards-types` to extend supported card schemas beyond the defaults. It targets modern Node.js environments (>= 18).
npm install creditcardsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates common usage patterns for credit card validation and formatting, including parsing, formatting, type detection, Luhn algorithm check, CVC validation, and expiration date checks. It also shows how to customize card types using `withTypes`.
Upgrade your Node.js runtime to version 18 or newer, or pin `creditcards` to a version less than 5.0.0 (e.g., `creditcards@^4`).
Always pass the output of `card.parse(number)` to `card.type()` or `card.isValid()` to prevent unexpected behavior with non-numeric characters.
Use `import Card from 'creditcards/card';` instead of `import { Card } from 'creditcards/card';` for individual module imports. Review the README for correct import patterns for specific sub-modules.For strict validation of a complete card number, ensure `eager` is `false` or omitted. Use `card.isValid()` for definitive validity checks.
Change `import { Card } from 'creditcards/card';` to `import Card from 'creditcards/card';`.Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import` statements. If sticking to CommonJS, verify the specific `creditcards` version and import path support `require()` for the module you're trying to access.
Always sanitize the card number using `card.parse()` before passing it to `card.type()` or `card.isValid()`: `card.type(card.parse(inputNumber))`.