{"id":13772,"library":"pip-requirements-js","title":"Pip Requirements File Parser for JavaScript","description":"pip-requirements-js is a pure JavaScript and TypeScript library designed to robustly parse `requirements.txt`, `constraints.txt`, and `requirements.in` files, strictly adhering to pip's documentation on the requirements file format and PEP 508 for dependency specification. The current stable version is 1.0.3, released on March 9, 2026. The project shows an active development cadence, with several releases in late 2025 and early 2026. Its key differentiator is the use of the Ohm.js parser generator, ensuring high fidelity to Python's pip parsing logic, including complex environment markers and file inclusions. It offers both 'full' and 'loose' parsing modes, with options to include source location information for advanced use cases like editor tooling.","status":"active","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/Twixes/pip-requirements-js","tags":["javascript","pip","python","requirements.txt","typescript"],"install":[{"cmd":"npm install pip-requirements-js","lang":"bash","label":"npm"},{"cmd":"yarn add pip-requirements-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add pip-requirements-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primary function for parsing full file content.","wrong":"const parsePipRequirementsFile = require('pip-requirements-js').parsePipRequirementsFile;","symbol":"parsePipRequirementsFile","correct":"import { parsePipRequirementsFile } from 'pip-requirements-js';"},{"note":"Used for parsing single lines. Note the named export.","wrong":"import parsePipRequirementsLine from 'pip-requirements-js/parsePipRequirementsLine';","symbol":"parsePipRequirementsLine","correct":"import { parsePipRequirementsLine } from 'pip-requirements-js';"},{"note":"Type definition for a parsed requirement. Always import types using 'import type'.","symbol":"Requirement","correct":"import type { Requirement } from 'pip-requirements-js';"},{"note":"Error class thrown for invalid requirements file syntax.","symbol":"RequirementsSyntaxError","correct":"import { RequirementsSyntaxError } from 'pip-requirements-js';"}],"quickstart":{"code":"import { parsePipRequirementsFile, RequirementsSyntaxError } from 'pip-requirements-js';\nimport type { Requirement, WithLocation } from 'pip-requirements-js';\n\nconst requirementsFileContent = `\n# My project dependencies\nDjango==4.2.0 ; python_version < '3.12'\nrequests>=2.28.0,<3.0.0 # HTTP library\n-r base.txt\nproject-core @ file:///local_libs/project_core-1.0-py3-none-any.whl\n`;\n\ntry {\n  const requirements: Requirement[] = parsePipRequirementsFile(requirementsFileContent);\n  console.log('Parsed requirements (full):', requirements);\n\n  const requirementsWithLocations: WithLocation<Requirement>[] = parsePipRequirementsFile(\n    requirementsFileContent,\n    { includeLocations: true }\n  );\n  console.log('Parsed requirements with locations:', requirementsWithLocations[0].location);\n} catch (error) {\n  if (error instanceof RequirementsSyntaxError) {\n    console.error('Syntax error in requirements file:', error.message);\n  } else {\n    console.error('An unexpected error occurred:', error);\n  }\n}\n","lang":"typescript","description":"Demonstrates parsing a multi-line `requirements.txt` string in both standard and location-inclusive modes, including basic error handling for syntax issues."},"warnings":[{"fix":"Review the API documentation for `parsePipRequirementsFile`, `parsePipRequirementsLine`, and their 'loose' counterparts to adapt to new return types like `WithLocation<T>` and `LooseProjectNameRequirement`.","message":"Version 1.0.0 introduced significant changes, including adding the `includeLocations` parsing option and refining the structure of objects returned by loose parsing functions. Code relying on previous output formats, especially for loose mode or without `includeLocations`, may need adjustments.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always check for `null` when processing the result of `parsePipRequirementsLine` before attempting to access requirement properties. Handle `RequirementsSyntaxError` separately for parsing failures.","message":"When using `parsePipRequirementsLine`, a `null` value is returned for lines that are syntactically valid but contain no actual requirement (e.g., empty lines or comment-only lines). This is different from `RequirementsSyntaxError` which is thrown for invalid syntax.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"When `includeLocations: true` is passed, iterate through the results as `const { data, location } of requirementsWithLocations;` or access `item.data` and `item.location` explicitly.","message":"The `includeLocations` option returns an array of `WithLocation<T>` objects, where the actual parsed data is nested under a `data` property. Ensure you access `item.data` to get the `Requirement` or `LooseProjectNameRequirement`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you need to process all types of pip requirements, including URL-based ones or references to other files, use the 'full' parsing functions (`parsePipRequirementsFile`, `parsePipRequirementsLine`).","message":"The `loose` parsing functions (`parsePipRequirementsFileLoosely`, `parsePipRequirementsLineLoosely`) specifically skip URL-based requirements and references to other requirement/constraints files. They return `LooseProjectNameRequirement` which is a subset of `Requirement`.","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"search_vec":"'1.0.3':49 '2025':67 '2026':54,70 '508':40 '9':53 'activ':59 'adher':28 'advanc':114 'cadenc':61 'case':116 'complex':92 'constraints.txt':23 'current':45 'depend':42 'design':18 'develop':60 'differenti':73 'document':32 'earli':69 'editor':118 'ensur':82 'environ':93 'fidel':84 'file':3,26,36,96 'format':37 'full':101 'generat':81 'high':83 'includ':91,109 'inclus':97 'inform':112 'javascript':6,14,120 'js':10 'key':72 'late':66 'librari':17 'like':117 'locat':111 'logic':90 'loos':103 'march':52 'marker':94 'mode':105 'offer':99 'ohm.js':79 'option':107 'pars':21,89,104 'parser':4,80 'pep':39 'pip':1,8,30,88,121 'pip-requirements-j':7 'project':56 'pure':13 'python':86,122 'releas':50,64 'requir':2,9,35 'requirements.in':25 'requirements.txt':22,123 'robust':20 'sever':63 'show':57 'sourc':110 'specif':43 'stabl':46 'strict':27 'tool':119 'typescript':16,124 'use':76,115 'version':47","created_at":"2026-04-20T01:56:14.242081+00:00","updated_at":"2026-04-20T01:56:14.242081+00:00","problems":[{"fix":"Ensure you are using Node.js v12+ and using `import` statements in an ESM context (e.g., in a `.mjs` file or with `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use `require()` with an ESM-only package or an older Node.js version that doesn't fully support ESM.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Review the exact error message, line, and column reported. Correct the syntax in your `requirements.txt` content to match pip's specifications. Common issues include malformed environment markers, incorrect version specifiers, or invalid options.","cause":"The input string passed to a parsing function does not conform to pip's requirements file format or PEP 508.","error":"RequirementsSyntaxError: Expected <some_grammar_rule> but found <unexpected_token> at line <num>, column <num>"},{"fix":"Add a null check before accessing properties of the parsed result: `const result = parsePipRequirementsLine(line); if (result) { /* process result.data or result.location */ }`","cause":"Attempting to access properties like `data` or `location` on the result of `parsePipRequirementsLine` when it returned `null` for a comment-only or empty line.","error":"TypeError: Cannot read properties of null (reading 'data')"}],"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/Twixes/pip-requirements-js","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/pip-requirements-js","openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops","serialization","testing"],"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}}