vCard Parser
The `vcard-parser` library is a utility for parsing vCard data into JavaScript objects and converting JavaScript objects back into vCard strings. It is designed to be a simple, standalone solution compatible with both Node.js environments and web browsers. Currently stable at version 1.0.0, the package primarily targets CommonJS module systems, as indicated by its usage examples. While it offers core vCard parsing and generation capabilities, its 'simple' nature might imply a focus on common vCard patterns rather than full support for every obscure or bleeding-edge vCard specification variant. As a 1.0.0 release, its release cadence is likely stable with infrequent updates, and its key differentiator is its straightforward, no-frills approach to vCard manipulation.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'parse') OR vCard.parse is not a function
cause Attempting to use `vCard.parse` when `vCard` was imported incorrectly or is `undefined`.fixEnsure the library is imported using `const vCard = require('vcard-parser');` in a CommonJS environment. For ESM, this package is not directly compatible. -
ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/vcard-parser/index.js
cause You are attempting to `require()` an ESM-only module in a CommonJS context, or `import` a CJS module without proper configuration.fixThis library is CommonJS. If you are in an ESM project, you cannot `import` it directly. You must use `require('vcard-parser')` within a CJS context or consider a different library. -
SyntaxError: Unexpected token 'export'
cause You are trying to run a JavaScript file with ESM `import`/`export` syntax in an environment that only supports CommonJS by default (e.g., older Node.js versions without `--experimental-modules` or `"type": "module"` in package.json).fixThis error is likely related to *your project's* configuration, not `vcard-parser` itself (which is CJS). Ensure your environment is configured for ESM if you are writing ESM, or revert to CommonJS syntax if not.
Warnings
- breaking The library exclusively uses CommonJS (`require`) syntax. Projects using ESM (`import`) will encounter module resolution errors when attempting to import directly.
- gotcha The parser expects specific line endings (`\r\n`). Malformed vCard strings with incorrect line endings (`\n` only) or other formatting issues might lead to incorrect parsing or errors.
- gotcha As a 'simple' parser, `vcard-parser` might not fully support all advanced or proprietary vCard properties, custom extensions, or complex encoding schemes defined in later vCard specifications (e.g., vCard 4.0).
Install
-
npm install vcard-parser -
yarn add vcard-parser -
pnpm add vcard-parser
Imports
- vCard
import vCard from 'vcard-parser';
const vCard = require('vcard-parser'); - parse
import { parse } from 'vcard-parser';const { parse } = require('vcard-parser'); // Destructuring after require - generate
import { generate } from 'vcard-parser';const { generate } = require('vcard-parser'); // Destructuring after require
Quickstart
const vCard = require('vcard-parser');
const raw = 'BEGIN:VCARD\r\n' +
'FN:Forrest Gump\r\n' +
'N:Gump;Forrest;;Mr.;\r\n' +
'TEL;TYPE=HOME:78884545247\r\n' +
'END:VCARD';
// Parse vCard data into a JavaScript object
const card = vCard.parse(raw);
console.log('Parsed vCard object:', JSON.stringify(card, null, 2));
/* Expected output:
{
"fn": [
{
"value": "Forrest Gump"
}
],
"n": [
{
"value": [
"Gump",
"Forrest",
"",
"Mr.",
""
]
}
],
"tel": [
{
"value": "78884545247",
"meta": {
"type": [
"HOME"
]
}
}
]
}
*/
// Generate vCard data from a JavaScript object
const generated = vCard.generate(card);
console.log('\nGenerated vCard string:\n', generated);
// Expected output: BEGIN:VCARD\r\nFN:Forrest Gump\r\nN:Gump;Forrest;;Mr.;\r\nTEL;TYPE=HOME:78884545247\r\nEND:VCARD