{"id":16048,"library":"gyp-parser","title":"GYP File Format Parser","description":"gyp-parser is a JavaScript library dedicated to parsing files written in the GYP (Generate Your Projects) format. GYP is a build system historically used by projects like Chromium and Node.js to generate native project files (e.g., Makefiles, Visual Studio projects). This package provides a robust, pure JavaScript implementation that converts GYP source strings into standard JavaScript objects. It serves as a secure and reliable alternative to using `eval()` for GYP content, adeptly handling complex GYP features such as implicit string concatenation, variable expansion, and dictionary merging that `eval()` might misinterpret or fail to process. The current stable version is 1.0.4, indicating a mature and specific tool that likely has an infrequent release cadence. Its key differentiator is its ability to parse the GYP format comprehensively without relying on `eval()` or performing any I/O operations, making it suitable for secure and isolated parsing tasks within applications.","status":"active","version":"1.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/addaleax/gyp-parser","tags":["javascript","gyp","parser","json","typescript"],"install":[{"cmd":"npm install gyp-parser","lang":"bash","label":"npm"},{"cmd":"yarn add gyp-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add gyp-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is primarily designed for ESM environments and ships TypeScript types. While a CommonJS `require` might work with transpilation, direct named ESM import is the idiomatic way.","wrong":"const parse = require('gyp-parser');","symbol":"parse","correct":"import { parse } from 'gyp-parser';"},{"note":"For CommonJS environments, ensure you destructure the named export. However, native ESM usage is recommended.","wrong":"const parse = require('gyp-parser'); // This tries to import the default export, which doesn't exist for 'parse'","symbol":"parse (CommonJS)","correct":"const { parse } = require('gyp-parser');"}],"quickstart":{"code":"import { parse } from 'gyp-parser';\n\n// Example GYP source with implicit string concatenation and a variable\nconst gypFileSource = `\n  {\n    'target_name': 'my_app',\n    'type': 'executable',\n    'variables': {\n      'common_flags': [ '-Wall', '-std=c++17' ]\n    },\n    'sources': [\n      'src/main.cc',\n      'src/utils.cc'\n    ],\n    'defines': [\n      'VERSION=1.0',\n      'DEBUG=1'\n    ],\n    'configurations': {\n      'Release': {\n        'defines': [ 'NDEBUG=1' ]\n      }\n    },\n    'actions': [\n      {\n        'action_name': 'generate_header',\n        'inputs': [ 'templates/config.h.in' ],\n        'outputs': [ '$$(RULE_DIR)/config.h' ],\n        'action': ['python', 'generate_config.py', '<@(_inputs)', '>', '<@(_outputs)'],\n      }\n    ]\n  }\n`;\n\ntry {\n  const parsedGyp = parse(gypFileSource);\n  console.log('Successfully parsed GYP data:');\n  console.log(JSON.stringify(parsedGyp, null, 2));\n\n  // Accessing a specific property\n  console.log(`\\nTarget Name: ${parsedGyp.target_name}`);\n  console.log(`Common Flags: ${parsedGyp.variables.common_flags.join(', ')}`);\n} catch (error) {\n  console.error('Failed to parse GYP data:', error.message);\n}","lang":"typescript","description":"This quickstart demonstrates parsing a complex GYP file string, including nested objects, arrays, variables, and actions, and then logging the resulting JavaScript object."},"warnings":[{"fix":"Always use `gyp-parser`'s `parse` function. It is specifically designed to handle GYP syntax securely and correctly, including features like implicit string concatenation and dictionary merging that `eval()` will not support natively.","message":"Directly using `eval()` to parse GYP file content is strongly discouraged and can lead to incorrect parsing, security vulnerabilities, and runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"You must provide the GYP file content as a string to the `parse` function. Handle file reading (e.g., using `fs.readFileSync` in Node.js) separately before passing the content to `gyp-parser`.","message":"This package is a parser only; it does not perform any I/O operations (e.g., reading files from disk).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the input string adheres to the GYP format. Errors for malformed GYP will be thrown as `SyntaxError` by the parser. Consult GYP documentation for exact syntax rules if encountering parsing issues.","message":"GYP syntax, while similar to JSON, has specific differences (e.g., unquoted keys, comments, string concatenation, variable expansion). Input must conform to GYP syntax, not strict JSON.","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":"Carefully review the GYP input string for any syntax inconsistencies. Ensure all strings are correctly quoted, dictionaries and arrays are properly terminated, and no invalid characters are present according to GYP rules.","cause":"The input GYP string contains a syntax error (e.g., unclosed quotes, incorrect punctuation, unsupported construct) that prevents successful parsing.","error":"SyntaxError: Unexpected token '...' in GYP file"},{"fix":"For CommonJS, use `const { parse } = require('gyp-parser');`. In ESM, ensure you are using `import { parse } from 'gyp-parser';`.","cause":"This typically occurs in CommonJS environments if `require('gyp-parser')` is assigned directly to `parse` without destructuring, or if there's an attempt to call a default export that doesn't exist.","error":"TypeError: parse is not a function"},{"fix":"Verify the GYP file content and the property path you are trying to access. Add checks (e.g., `if (parsedData && parsedData.some_key)`) to handle cases where properties might be missing or optional.","cause":"Attempting to access a property on the parsed GYP object that does not exist or is undefined in the GYP structure.","error":"TypeError: Cannot read properties of undefined (reading 'some_key')"}],"ecosystem":"npm"}