{"id":10961,"library":"gradle-to-js","title":"Gradle to JavaScript Object Parser","description":"gradle-to-js is a JavaScript library designed to parse Gradle build files (`.gradle`) into plain JavaScript objects. Currently at version 2.0.1, it offers a 'quick & dirty' approach, focusing on extracting key-value pairs and block structures rather than executing or accurately representing runtime evaluations within the Gradle script. This means complex logic, closures, or dynamic expressions found in Gradle files will not be processed, resulting in a simplified, static representation. The library primarily supports CommonJS imports and is best suited for scenarios where a basic, structural understanding of a Gradle file is needed, rather than a full, executable interpretation. Its release cadence appears infrequent, and it is likely in maintenance mode, reflecting its specific and somewhat limited parsing scope.","status":"maintenance","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/ninetwozero/gradle-to-js","tags":["javascript","gradle","parsing"],"install":[{"cmd":"npm install gradle-to-js","lang":"bash","label":"npm"},{"cmd":"yarn add gradle-to-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add gradle-to-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses CommonJS `require()`. Direct ESM `import` may require bundler configuration or dynamic import, and is not officially supported.","wrong":"import g2js from 'gradle-to-js/lib/parser';","symbol":"parser","correct":"const g2js = require('gradle-to-js/lib/parser');"},{"note":"The `parser` module is imported as a single object (e.g., `g2js`), and `parseFile` is a method on that object, not a named export. Promises are used for asynchronous operations.","wrong":"const { parseFile } = require('gradle-to-js/lib/parser');","symbol":"parseFile","correct":"g2js.parseFile('path/to/buildfile').then(...);"},{"note":"Similar to `parseFile`, `parseText` is a method of the imported `parser` object, not a named export. It parses a Gradle-like string directly.","wrong":"const { parseText } = require('gradle-to-js/lib/parser');","symbol":"parseText","correct":"g2js.parseText('key \"value\"').then(...);"}],"quickstart":{"code":"const g2js = require('gradle-to-js/lib/parser');\nconst fs = require('fs');\nconst path = require('path');\n\n// Create a dummy Gradle file for demonstration\nconst tempGradleFile = path.join(__dirname, 'temp.gradle');\nfs.writeFileSync(tempGradleFile, `\napply plugin: 'java'\n\nsourceCompatibility = 1.8\ntargetCompatibility = 1.8\n\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    implementation 'org.slf4j:slf4j-api:1.7.30'\n    testImplementation 'junit:junit:4.13'\n}\n\nmyBlock {\n    key1 \"value1\"\n    key2 123\n}\n`);\n\ng2js.parseFile(tempGradleFile)\n  .then(function(representation) {\n    console.log('Parsed Gradle file representation:');\n    console.log(JSON.stringify(representation, null, 2));\n  })\n  .catch(function(error) {\n    console.error('Error parsing Gradle file:', error);\n  })\n  .finally(() => {\n    // Clean up the dummy file\n    fs.unlinkSync(tempGradleFile);\n  });\n","lang":"javascript","description":"This example demonstrates how to parse a Gradle build file into a JavaScript object using `parseFile` and then logs the structured representation. It includes setup for a temporary file and error handling."},"warnings":[{"fix":"Understand that this library is for static structural parsing only. For full Gradle script evaluation, consider using Gradle itself or a more sophisticated Groovy/Kotlin parser.","message":"The parser is explicitly 'quick & dirty' and does not evaluate Gradle runtime logic, Groovy closures, or dynamic expressions within the build file. It produces a static, structural representation, which may not fully reflect the build file's behavior during actual Gradle execution.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use CommonJS `require()` syntax: `const g2js = require('gradle-to-js/lib/parser');`. If you must use it in an ESM context, consider dynamic `import('gradle-to-js/lib/parser')` or a bundler.","message":"This library is primarily a CommonJS module. Direct `import` statements (ESM syntax) will likely lead to `SyntaxError: Cannot use import statement outside a module` in Node.js projects not explicitly configured for CJS/ESM interoperation.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always import the specific parser module: `require('gradle-to-js/lib/parser')`.","message":"The primary entry point for the parser functionality is `gradle-to-js/lib/parser`, not the root `gradle-to-js` package. Attempting to `require('gradle-to-js')` directly will likely result in an object without the `parseFile` or `parseText` methods.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For CLI usage, execute `node node_modules/gradle-to-js/index.js <path-to-file>` or verify `package.json` for a `bin` entry and use the appropriate command.","message":"The CLI usage examples demonstrate running the `index.js` directly via `./index.js` or `node ./index.js`. This implies the CLI might not be installed as a globally available command without manual `npm link` or an explicit `bin` entry in `package.json`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are importing from the correct path: `const g2js = require('gradle-to-js/lib/parser');`","cause":"Attempting to call `parseFile` on an object imported from the root `gradle-to-js` package instead of the `lib/parser` module.","error":"TypeError: g2js.parseFile is not a function"},{"fix":"Use CommonJS `require()` syntax: `const g2js = require('gradle-to-js/lib/parser');`","cause":"Trying to use ESM `import` syntax for `gradle-to-js` in a Node.js environment that expects CommonJS modules.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Double-check the file path. Ensure it's correct and that the Node.js process has read permissions for the specified file.","cause":"The file path provided to `g2js.parseFile()` does not point to an existing or accessible file.","error":"Error: ENOENT: no such file or directory, open 'path/to/buildfile'"}],"ecosystem":"npm"}