Node.js guide
How to Parse a Resume in Node.js
Get data out of PDF and DOCX resumes with JavaScript: first with open-source packages, then with a resume parser API that returns structured JSON. Every snippet was run against a real file, and the outputs are what it printed. Prefer Python? See the Python version.
What you need
- Node.js 18 or newer (the examples use ES modules: save them as
.mjs) npm install pdf-parse mammothfor step 1- A sample resume (PDF or DOCX)
1Extract the text from PDF and DOCX files
pdf-parse reads text-based PDFs and mammoth reads Word files. Importing pdf-parse/lib/pdf-parse.js directly avoids a debug mode in the package's entry file.
import { readFile } from "node:fs/promises";
import pdf from "pdf-parse/lib/pdf-parse.js";
import mammoth from "mammoth";
async function extractText(path) {
const buffer = await readFile(path);
if (path.toLowerCase().endsWith(".pdf")) {
return (await pdf(buffer)).text;
}
if (path.toLowerCase().endsWith(".docx")) {
return (await mammoth.extractRawText({ buffer })).value;
}
throw new Error(`Unsupported file type: ${path}`);
}
const text = await extractText("resume.pdf");
console.log(text.slice(0, 300));Maria Lopez
Senior Backend Engineer
San Francisco, CA | [email protected] | +1 415 555 0142
linkedin.com/in/marialopez-example | github.com/marialoYou get the words back, but not the structure: pdf-parse pads lines with spaces to mimic the page layout, columns can interleave, and nothing tells you which line is a job title and which is an employer.
2Pull out contact details with regular expressions
Fields with a predictable shape are easy to match:
const EMAIL = /[\w.+-]+@[\w-]+\.[\w.-]+/;
const PHONE = /\+?\d[\d\s().-]{7,}\d/;
const LINKEDIN = /linkedin\.com\/in\/[\w-]+/i;
function extractContact(text) {
return {
email: text.match(EMAIL)?.[0] ?? null,
phone: text.match(PHONE)?.[0].trim() ?? null,
linkedin: text.match(LINKEDIN)?.[0] ?? null,
};
}
console.log(extractContact(text));{
email: '[email protected]',
phone: '+1 415 555 0142',
linkedin: 'linkedin.com/in/marialopez-example'
}Work history, dates, education and skills have no fixed format, so regex stops being useful here. See how to build a resume parser for the options and their trade-offs.
3Get structured JSON from a resume parser API
For the full structure, send the file to the CV Parser Pro resume parser API. Create a free account to get an API key (10 free parses, no credit card) and set it as CVPP_API_KEY. This function uploads a file and waits for the result:
import { readFile } from "node:fs/promises";
import { basename } from "node:path";
const API = "https://cvparserpro.com/api/v1";
const headers = { "X-API-Key": process.env.CVPP_API_KEY };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function parseResume(path, timeoutMs = 120_000) {
const form = new FormData();
form.append("files", new Blob([await readFile(path)]), basename(path));
const upload = await fetch(`${API}/parse`, { method: "POST", headers, body: form });
if (!upload.ok) throw new Error(`Upload failed: ${upload.status} ${await upload.text()}`);
const { candidates } = await upload.json();
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const res = await fetch(`${API}/candidates/${candidates[0].id}`, { headers });
const { data } = await res.json();
if (data.parsing_status === "completed") return data;
if (data.parsing_status === "failed") throw new Error(JSON.stringify(data.parsing_errors));
await sleep(3000);
}
throw new Error(`Resume ${candidates[0].id} was not parsed in time`);
}
const resume = await parseResume("resume.pdf");
console.log(`${resume.full_name} - ${resume.current_title}`);
for (const job of resume.work_history) {
console.log(`${job.start_date} to ${job.end_date}: ${job.title} at ${job.company}`);
}Maria Lopez - Senior Backend Engineer
2021-03 to present: Senior Backend Engineer at Northwind Logistics
2018-01 to 2021-02: Software Engineer at Brightline HealthDates come back normalised to YYYY-MM. The response also has education, certifications, skills, spoken languages, a summary and per-field confidence scores; see every field in the response. If you would rather not poll, configure a webhook and receive a signed event when each resume is done.
4Parse a folder of resumes into a CSV
Add this below parseResume to turn a resumes/ folder into a spreadsheet:
import { readdir, writeFile } from "node:fs/promises";
import { extname, join } from "node:path";
const csv = (v) => `"${String(v ?? "").replace(/"/g, '""')}"`;
const rows = [["file", "name", "email", "title", "years", "skills"].join(",")];
for (const file of (await readdir("resumes")).sort()) {
if (![".pdf", ".docx"].includes(extname(file).toLowerCase())) continue;
try {
const r = await parseResume(join("resumes", file));
rows.push([file, r.full_name, r.email, r.current_title, r.years_of_experience, (r.skills ?? []).join(", ")].map(csv).join(","));
} catch (err) {
console.log(`${file}: ${err.message}`);
}
}
await writeFile("candidates.csv", rows.join("\n") + "\n");
console.log(`Saved ${rows.length - 1} candidates to candidates.csv`);file,name,email,title,years,skills
"maria.docx","Maria Lopez","[email protected]","Senior Backend Engineer","7","Python, Go, PostgreSQL, Kubernetes, AWS, Apache Kafka, Docker"
"maria.pdf","Maria Lopez","[email protected]","Senior Backend Engineer","7","Python, Go, PostgreSQL, Kubernetes, AWS, Apache Kafka, Docker"Files that cannot be parsed are reported and skipped, and their credit is refunded. For large backlogs see bulk resume parsing.