{"id":16189,"library":"rdf-parser-csvw","title":"RDF/JS CSV on the Web Parser","description":"rdf-parser-csvw is a JavaScript library designed to parse CSV (Comma Separated Values) data according to the CSV on the Web (CSVW) W3C recommendation, converting it into RDF/JS Quads. It leverages the RDF/JS Stream interface, allowing for efficient, asynchronous processing of large CSV files by consuming a stream of strings and emitting a stream of parsed RDF quads. The library's current stable version is 1.1.0, with releases typically following a feature-driven cadence rather than strict timeboxes. A key differentiator is its strict adherence to the RDF/JS specification for data factories and stream interfaces, ensuring broad compatibility within the RDF/JS ecosystem. It requires explicit CSVW metadata (as an RDF/JS Dataset) and a base IRI for proper conversion. Options include specifying a custom RDF/JS data factory, an alternative timezone for date/time parsing, and error-handling preferences such as `relaxColumnCount` to ignore column count mismatches or `skipLinesWithError` for debugging noisy datasets, though the latter is advised against for production use.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/rdf-ext/rdf-parser-csvw","tags":["javascript","rdf","rdf-ext","csv","csvw"],"install":[{"cmd":"npm install rdf-parser-csvw","lang":"bash","label":"npm"},{"cmd":"yarn add rdf-parser-csvw","lang":"bash","label":"yarn"},{"cmd":"pnpm add rdf-parser-csvw","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Default RDF/JS data factory, can be overridden.","package":"@rdfjs/data-model","optional":true},{"reason":"Required for creating the `metadata` Dataset, or `rdf-ext` as a common alternative.","package":"@rdfjs/dataset","optional":false},{"reason":"Commonly used for RDF/JS data factory and dataset implementation in the ecosystem, often used alongside this parser. Can substitute for @rdfjs/data-model and @rdfjs/dataset.","package":"rdf-ext","optional":true}],"imports":[{"note":"The parser is exported as a named class `Parser` in ESM. Do not use default import.","wrong":"import Parser from 'rdf-parser-csvw'","symbol":"Parser","correct":"import { Parser } from 'rdf-parser-csvw'"},{"note":"For CommonJS, access the `Parser` class as a named property from the module export.","wrong":"const Parser = require('rdf-parser-csvw')","symbol":"Parser (CommonJS)","correct":"const { Parser } = require('rdf-parser-csvw')"},{"note":"When only importing the type for TypeScript, use `import type` for clarity and bundler optimization.","symbol":"Parser (Type Import)","correct":"import type { Parser } from 'rdf-parser-csvw'"}],"quickstart":{"code":"import { Readable } from 'stream';\nimport { Parser } from 'rdf-parser-csvw';\nimport rdf from 'rdf-ext'; // A common RDF/JS implementation for DataFactory and Dataset\n\nasync function parseCsvw() {\n  const csvString = `Name,Age\\nAlice,30\\nBob,25\\nCharlie,35`;\n  const baseIRI = 'http://example.org/data/';\n\n  // Construct a minimal CSVW metadata Dataset using rdf-ext\n  const metadataDataset = rdf.dataset();\n  const ex = rdf.namedNode(baseIRI);\n  const csvw = rdf.namedNode('http://www.w3.org/ns/csvw#');\n  const rdfType = rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');\n\n  const tableGroup = rdf.blankNode();\n  const table = rdf.blankNode();\n  const column1 = rdf.blankNode();\n  const column2 = rdf.blankNode();\n\n  metadataDataset.add(rdf.quad(ex.file, rdfType, csvw.TableGroup));\n  metadataDataset.add(rdf.quad(ex.file, csvw.table, table));\n  metadataDataset.add(rdf.quad(table, rdfType, csvw.Table));\n  metadataDataset.add(rdf.quad(table, csvw.url, rdf.namedNode(`${baseIRI}data.csv`)));\n\n  // Define columns based on CSV headers\n  metadataDataset.add(rdf.quad(table, csvw.column, column1));\n  metadataDataset.add(rdf.quad(column1, csvw.name, rdf.literal('Name')));\n  metadataDataset.add(rdf.quad(column1, csvw.datatype, csvw.string));\n\n  metadataDataset.add(rdf.quad(table, csvw.column, column2));\n  metadataDataset.add(rdf.quad(column2, csvw.name, rdf.literal('Age')));\n  metadataDataset.add(rdf.quad(column2, csvw.datatype, csvw.integer));\n\n  // Instantiate the parser with required options\n  const parser = new Parser({\n    metadata: metadataDataset,\n    baseIRI: baseIRI,\n    factory: rdf // Use rdf-ext's data factory\n  });\n\n  // Create a readable stream from the CSV string\n  const csvStream = Readable.from([csvString]);\n\n  console.log('Starting CSVW parsing...');\n  // Import the CSV stream and get a stream of RDF quads\n  const quadStream = parser.import(csvStream);\n\n  // Consume and log the parsed quads\n  for await (const quad of quadStream) {\n    console.log(quad.toString());\n  }\n  console.log('Finished parsing.');\n}\n\nparseCsvw().catch(console.error);","lang":"typescript","description":"Demonstrates how to instantiate the `Parser` class, construct a minimal CSVW metadata RDF/JS Dataset, and parse a CSV string into RDF quads using streams."},"warnings":[{"fix":"Ensure the `metadata` option is always provided with a valid RDF/JS Dataset containing CSVW definitions.","message":"The `metadata` option is strictly required in the parser constructor or `.import` method. It must be an RDF/JS Dataset representing the CSV on the Web metadata for your CSV file.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Provide a valid string `baseIRI` in the parser constructor options or the `.import` method options.","message":"The `baseIRI` option is strictly required to create Named Nodes from relative IRIs within the CSV data.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Avoid `skipLinesWithError` in production. Instead, pre-process CSV data to ensure correctness or handle errors robustly.","message":"The `skipLinesWithError` option is primarily for debugging purposes and should not be used in production environments, as it can lead to silent data loss or inconsistent graph generation from malformed CSV lines.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set `relaxColumnCount: true` in the parser options if column count mismatches should not halt parsing.","message":"By default, the parser will throw an error if a row's column count does not match the headers. Use `relaxColumnCount` if you expect and want to tolerate such discrepancies.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Pass an RDF/JS `Dataset` object containing CSVW metadata to the `metadata` option.","cause":"The `metadata` option was not provided during parser instantiation or the `.import` call.","error":"Error: The metadata option is required"},{"fix":"Provide a string value for `baseIRI` in the parser options, e.g., `new Parser({ baseIRI: 'http://example.org/' })`.","cause":"The `baseIRI` option was not provided, which is essential for resolving relative IRIs.","error":"Error: The baseIRI option is required"},{"fix":"Either fix the malformed CSV data, or set `relaxColumnCount: true` in the parser options to ignore these errors.","cause":"A row in the CSV stream has a different number of columns than expected, and `relaxColumnCount` is not enabled.","error":"Error: Column count mismatch in row X"},{"fix":"Ensure you create an instance of the `Parser` class using `new Parser(options)` before calling `.import()`.","cause":"The `Parser` class was not correctly instantiated, or `import` was called on the class itself instead of an instance.","error":"TypeError: parser.import is not a function"}],"ecosystem":"npm"}