{"id":27794,"library":"gcode-parser","title":"gcode-parser","description":"G-code parser for Node.js, current stable version 2.2.0. Maintained as part of the cncjs ecosystem. Provides synchronous and asynchronous parsing of G-code strings, lines, files, and streams. Key differentiators: supports line modes (original, stripped, compact) and flatten option for word representation. Used widely in CNC toolpath generation and visualization.","status":"active","version":"2.2.0","language":"javascript","source_language":"en","source_url":"git@github.com:cncjs/gcode-parser","tags":["javascript","cnc","gcode"],"install":[{"cmd":"npm install gcode-parser","lang":"bash","label":"npm"},{"cmd":"yarn add gcode-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add gcode-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CJS module; ESM import not supported.","wrong":"import parser from 'gcode-parser';","symbol":"default","correct":"const parser = require('gcode-parser');"},{"note":"Must call on the parser object.","wrong":"parseLine('G0 X0 Y0');","symbol":"parseLine","correct":"parser.parseLine('G0 X0 Y0');"},{"note":"Callback is required; returns an EventEmitter with 'data' and 'end' events.","wrong":"parser.parseFile('file.nc');","symbol":"parseFile","correct":"parser.parseFile('file.nc', callback);"},{"note":"Synchronous variant available for parseString and parseFile.","wrong":"","symbol":"parseStringSync","correct":"const results = parser.parseStringSync('G0 X0');"}],"quickstart":{"code":"const fs = require('fs');\nconst parser = require('gcode-parser');\n\n// Parse a single line\nconsole.log(parser.parseLine('G0 X0 Y0'));\n// => { line: 'G0 X0 Y0', words: [ [ 'G', 0 ], [ 'X', 0 ], [ 'Y', 0 ] ] }\n\n// Parse a file asynchronously\nparser.parseFile('example.nc', (err, results) => {\n  if (err) throw err;\n  console.log(results);\n});\n\n// Parse a string synchronously\nconst str = fs.readFileSync('example.nc', 'utf8');\nconst results = parser.parseStringSync(str);\nconsole.log(results);","lang":"javascript","description":"Demonstrates parsing a single line, async file parsing, and sync string parsing."},"warnings":[{"fix":"Use callback for results, and attach listeners for 'data' or 'end' if needed.","message":"parseFile and parseString callbacks accept (err, results) but also return an EventEmitter. Not handling both can lead to missed events.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Correct: parser.parseLine('G0 X0', { flatten: true }); Incorrect: parser.parseLine('G0 X0', true);","message":"parseLine with options object: lineMode and flatten should be passed as second argument. Omitting options uses defaults.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Access word[0] for letter and word[1] for value, or use flatten: true to get strings like 'G0'.","message":"Result words are arrays of [letter, number] in non-flatten mode. Forgetting this can cause type errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use const parser = require('gcode-parser'); and run in Node.js CJS environment.","cause":"Using ES module import syntax with require or missing installation.","error":"TypeError: parser.parseFile is not a function"},{"fix":"Check for empty array before accessing elements: if (result.words.length > 0) { ... }","cause":"Assuming words are always present in result, but empty lines return { words: [] }.","error":"TypeError: Cannot read property '0' of undefined"},{"fix":"Always provide a callback: parser.parseFile('file.nc', (err, result) => { ... });","cause":"Calling parseFile without a second argument (callback) or passing a non-function.","error":"TypeError: callback is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}