{"id":13986,"library":"sdf-parser","title":"SDF Parser","description":"sdf-parser is a JavaScript/TypeScript library designed to parse Structure-Data File (SDF) format files, which are commonly used in cheminformatics to represent chemical structures and associated data. It efficiently converts the content of SDF files into an array of JavaScript objects, where each object represents a chemical entry including its molfile and associated data fields. The current stable version is 8.0.0. The package maintains an active release cadence, with frequent updates. Key features include robust parsing with options for field inclusion/exclusion, custom data transformations via modifiers, and filtering capabilities based on entry properties. For processing large SDF files, it offers an `iterator` API that leverages web streams for memory-efficient handling, compatible with both Node.js and browser environments.","status":"active","version":"8.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/cheminfo/sdf-parser","tags":["javascript","sdf","parser","molfile","v2000","v3000","mdl"],"install":[{"cmd":"npm install sdf-parser","lang":"bash","label":"npm"},{"cmd":"yarn add sdf-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add sdf-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v8.0.0, sdf-parser is an ESM-only package. CommonJS `require` syntax will not work.","wrong":"const { parse } = require('sdf-parser');","symbol":"parse","correct":"import { parse } from 'sdf-parser';"},{"note":"v8.0.0 is ESM-only. The `iterator` function, introduced in v6.0.0, requires piping input through a `TextDecoderStream` since v7.0.0 for proper text decoding.","wrong":"const { iterator } = require('sdf-parser');","symbol":"iterator","correct":"import { iterator } from 'sdf-parser';"},{"note":"For TypeScript projects, import `SDFEntry` type for type-safe handling of parsed SDF objects.","symbol":"SDFEntry","correct":"import type { SDFEntry } from 'sdf-parser';"}],"quickstart":{"code":"import { parse, iterator } from 'sdf-parser';\nimport { Readable } from 'node:stream'; // For Node.js to create a stream from string\nimport { TextDecoderStream } from 'node:stream/web'; // For Node.js compatibility with Web Streams API\n\n// Example SDF content with multiple entries\nconst sdfData = `\nMOLFILE\n  -OEChem-0104201804252D\n\n  5  5  0  0  0  0  0  0  0  0999 V2000\n    0.0000    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    1.5000    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    0.0000    1.5000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    1.5000    1.5000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    3.0000    0.0000    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0\n  1  2  1  0  0  0  0\n  1  3  1  0  0  0  0\n  2  4  1  0  0  0  0\n  3  4  1  0  0  0  0\n  2  5  1  0  0  0  0\nM  END\n> <ID>\nCHEM_1\n> <CLogP>\n2.5\n$$$$\nMOLFILE\n  -OEChem-0104201804252D\n\n  3  2  0  0  0  0  0  0  0  0999 V2000\n    0.0000    0.0000    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0\n    1.5000    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    3.0000    0.0000    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0\n  1  2  1  0  0  0  0\n  2  3  1  0  0  0  0\nM  END\n> <ID>\nCHEM_2\n> <CLogP>\n-1.2\n$$$$\n`;\n\n// --- Example 1: Parsing a full SDF string synchronously ---\nconst parsedEntries = parse(sdfData, {\n  include: ['ID', 'CLogP'],\n  modifiers: {\n    CLogP: (value: string) => parseFloat(value) // Convert CLogP field to a number\n  },\n  filter: (entry: any) => entry.CLogP > 0 // Keep only entries with positive CLogP\n});\n\nconsole.log('Parsed Entries (filtered by CLogP > 0):', parsedEntries);\n\n// --- Example 2: Iterating over an SDF stream (for large files) ---\nasync function processSDFStream() {\n  // In Node.js, create a ReadableStream from string data.\n  // For actual files, use `fs.createReadStream('your.sdf').pipeThrough(new TextDecoderStream())`\n  const streamFromData = Readable.from([sdfData])\n    .pipeThrough(new TextDecoderStream()); // Crucial for v7.0.0+ for iterator\n\n  const iteratedEntries = [];\n  try {\n    for await (const entry of iterator(streamFromData)) {\n      iteratedEntries.push(entry);\n    }\n    console.log('Iterated Entries (all from stream):', iteratedEntries);\n  } catch (error) {\n    console.error('Error during stream iteration:', error);\n  }\n}\n\nprocessSDFStream();","lang":"typescript","description":"This quickstart demonstrates both the synchronous `parse` function for smaller SDF strings, including options for filtering and modifying fields, and the asynchronous `iterator` function for efficient processing of larger SDF data via streams, highlighting the `TextDecoderStream` requirement."},"warnings":[{"fix":"Update all `require()` calls to `import` statements (e.g., `const { parse } = require('sdf-parser');` becomes `import { parse } from 'sdf-parser';`). Ensure your project is configured for ESM or use a bundler that supports ESM.","message":"Version 8.0.0 migrated the library to TypeScript and set `type: module` in `package.json`. This means the package is now ESM-only and CommonJS `require()` statements will no longer work.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"When using `iterator`, ensure your stream is processed with `stream.pipeThrough(new TextDecoderStream())`. For example: `for await (const entry of iterator(file.stream().pipeThrough(new TextDecoderStream())))`.","message":"Version 7.0.0 introduced a breaking change to the `iterator` function, requiring the input stream to be piped through a `TextDecoderStream`. This was done for browser compatibility and ensures correct text decoding.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Migrate any usage of the `stream` function to the `iterator` function. The `iterator` function provides an asynchronous iterable interface for processing SDF entries one by one.","message":"Version 6.0.0 removed the `stream` function entirely. This function was replaced by the more flexible and memory-efficient `iterator` function.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"For gzipped files, the stream pipeline should look like: `file.stream().pipeThrough(new DecompressionStream('gzip')).pipeThrough(new TextDecoderStream())`.","message":"When working with compressed SDF files (e.g., `.sdf.gz`), you'll need to pipe the stream through a decompression stream *before* piping it through `TextDecoderStream` and feeding it to `iterator`.","severity":"gotcha","affected_versions":">=6.0.0"}],"env_vars":null,"search_vec":"'8.0.0':66 'activ':71 'api':108 'array':43 'associ':31,58 'base':95 'browser':123 'cadenc':73 'capabl':94 'chemic':28,52 'cheminformat':25 'common':22 'compat':118 'content':37 'convert':35 'current':62 'custom':87 'data':15,32,59,88 'design':10 'effici':34,116 'entri':53,97 'environ':124 'featur':78 'field':60,85 'file':16,19,40,103 'filter':93 'format':18 'frequent':75 'handl':117 'includ':54,79 'inclusion/exclusion':86 'iter':107 'javascript':45,125 'javascript/typescript':8 'key':77 'larg':101 'leverag':110 'librari':9 'maintain':69 'mdl':131 'memori':115 'memory-effici':114 'modifi':91 'molfil':56,128 'node.js':121 'object':46,49 'offer':105 'option':83 'packag':68 'pars':12,81 'parser':2,5,127 'process':100 'properti':98 'releas':72 'repres':27,50 'robust':80 'sdf':1,4,17,39,102,126 'sdf-parser':3 'stabl':63 'stream':112 'structur':14,29 'structure-data':13 'transform':89 'updat':76 'use':23 'v2000':129 'v3000':130 'version':64 'via':90 'web':111","created_at":"2026-04-20T01:57:20.814486+00:00","updated_at":"2026-04-20T01:57:20.814486+00:00","problems":[{"fix":"Change `const { symbol } = require('sdf-parser');` to `import { symbol } from 'sdf-parser';` and ensure your project is configured for ES Modules.","cause":"Attempting to use CommonJS `require` syntax with sdf-parser v8.0.0 or later, which is an ESM-only package.","error":"ReferenceError: require is not defined"},{"fix":"Modify your stream pipeline to include `pipeThrough(new TextDecoderStream())` before passing it to `iterator`. Example: `iterator(yourStream.pipeThrough(new TextDecoderStream()))`.","cause":"Attempting to use the `iterator` function in v7.0.0+ without piping the input stream through `new TextDecoderStream()`.","error":"TypeError: 'for await...of' expects an async iterable, but received an object"},{"fix":"Replace calls to the deprecated `stream` function with the `iterator` function. `iterator` provides similar functionality but with an asynchronous iterable interface.","cause":"Trying to use the `stream` function which was removed in sdf-parser v6.0.0.","error":"The stream() method is not defined on this object."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/cheminfo/sdf-parser","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/sdf-parser","openapi_spec":null,"status_page":null,"smithery":null,"categories":["data","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}