Credit Card Utility Methods
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).
Common errors
-
TypeError: (0 , creditcards_card__WEBPACK_IMPORTED_MODULE_0__.Card) is not a function
cause Attempting to import a default export from a sub-module as a named export.fixChange `import { Card } from 'creditcards/card';` to `import Card from 'creditcards/card';`. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... Not supported
cause Attempting to `require()` an ESM-only module or using CJS `require` in an ESM project for a package that strictly enforces ESM.fixEnsure 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. -
card.type is undefined when passing a number like '4242 4242 4242 4242'
cause `card.type` and `card.isValid` expect a purely numeric string, not one with spaces or other characters.fixAlways sanitize the card number using `card.parse()` before passing it to `card.type()` or `card.isValid()`: `card.type(card.parse(inputNumber))`.
Warnings
- breaking Version 5.0.0 elevates the minimum Node.js requirement to >= 18. Applications running on older Node.js versions will need to upgrade their environment or stick to an earlier major version of `creditcards`.
- gotcha When using `card.type()` or `card.isValid()`, ensure the card number is sanitized first using `card.parse()`. These methods expect a purely numeric string without spaces or punctuation.
- gotcha Individual module imports (e.g., `creditcards/card`) typically export a default function or object, not named exports. Incorrectly using named imports for these paths will lead to `TypeError: (0 , creditcards_card__WEBPACK_IMPORTED_MODULE_0__.Card) is not a function` or similar errors.
- gotcha The `eager` parameter for `card.type(number, eager)` will match partial card numbers, which can be useful for UI feedback during input but should not be relied upon for final, strict validation of a complete card number.
Install
-
npm install creditcards -
yarn add creditcards -
pnpm add creditcards
Imports
- card
const { card } = require('creditcards');import { card } from 'creditcards'; - cvc
const { cvc } = require('creditcards');import { cvc } from 'creditcards'; - expiration
const { expiration } = require('creditcards');import { expiration } from 'creditcards'; - Card
import { Card } from 'creditcards/card';import Card from 'creditcards/card';
Quickstart
import { card, cvc, expiration, withTypes } from 'creditcards';
import { visa, mastercard } from 'creditcards-types';
// --- Card Utilities ---
const rawCardNumber = ' 4242-4242-4242-4242 ';
const parsedCardNumber = card.parse(rawCardNumber); // '4242424242424242'
const formattedVisa = card.format(parsedCardNumber); // '4242 4242 4242 4242'
const cardType = card.type(parsedCardNumber); // 'visa'
const isValidLuhn = card.luhn(parsedCardNumber); // true
const isValidCard = card.isValid(parsedCardNumber); // true
const isValidVisa = card.isValid(parsedCardNumber, 'visa'); // true
console.log(`Parsed Card: ${parsedCardNumber}`);
console.log(`Formatted Card: ${formattedVisa}`);
console.log(`Card Type: ${cardType}`);
console.log(`Luhn Valid: ${isValidLuhn}`);
// --- CVC Utilities ---
const cvcValue = '123';
const isValidCVC = cvc.isValid(cvcValue, 'visa'); // true
console.log(`CVC Valid for Visa: ${isValidCVC}`);
// --- Expiration Utilities ---
const currentMonth = new Date().getMonth() + 1;
const currentYear = new Date().getFullYear();
const futureYear = currentYear + 5;
const isMonthValid = expiration.month.isValid(currentMonth); // true
const isYearValid = expiration.year.isValid(futureYear); // true
const isPast = expiration.isPast(currentMonth - 1, currentYear); // true (assuming currentMonth > 1)
console.log(`Expiration Month Valid: ${isMonthValid}`);
console.log(`Expiration Year Valid: ${isYearValid}`);
console.log(`Is past (last month): ${isPast}`);
// --- Using custom types (requires 'creditcards-types') ---
const customCardModule = withTypes([visa, mastercard]);
const isAmexWithCustomTypes = customCardModule.card.isValid('378282246310005', 'american-express'); // false if only visa/mastercard are included
console.log(`Is Amex valid with custom types (only Visa/MC): ${isAmexWithCustomTypes}`);