Registry / data / gcode-parser

gcode-parser

JSON →
library2.2.0jsnpmunverified

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.

npm install gcode-parser
INSTALL
IMPORT
SIG · GCODE-PARSER
G
gcode-parser
datajavascriptv2.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const parser = require('gcode-parser');
import parser from 'gcode-parser';
CJS module; ESM import not supported.
parseLine
parser.parseLine('G0 X0 Y0');
parseLine('G0 X0 Y0');
Must call on the parser object.
parseFile
parser.parseFile('file.nc', callback);
parser.parseFile('file.nc');
Callback is required; returns an EventEmitter with 'data' and 'end' events.
parseStringSync
const results = parser.parseStringSync('G0 X0');
Synchronous variant available for parseString and parseFile.

Demonstrates parsing a single line, async file parsing, and sync string parsing.

const fs = require('fs'); const parser = require('gcode-parser'); // Parse a single line console.log(parser.parseLine('G0 X0 Y0')); // => { line: 'G0 X0 Y0', words: [ [ 'G', 0 ], [ 'X', 0 ], [ 'Y', 0 ] ] } // Parse a file asynchronously parser.parseFile('example.nc', (err, results) => { if (err) throw err; console.log(results); }); // Parse a string synchronously const str = fs.readFileSync('example.nc', 'utf8'); const results = parser.parseStringSync(str); console.log(results);
Debug
Known issues
gotchaparseFile and parseString callbacks accept (err, results) but also return an EventEmitter. Not handling both can lead to missed events.
fix
Use callback for results, and attach listeners for 'data' or 'end' if needed.
affects: >=1.0.0
gotchaparseLine with options object: lineMode and flatten should be passed as second argument. Omitting options uses defaults.
fix
Correct: parser.parseLine('G0 X0', { flatten: true }); Incorrect: parser.parseLine('G0 X0', true);
affects: >=2.0.0
gotchaResult words are arrays of [letter, number] in non-flatten mode. Forgetting this can cause type errors.
fix
Access word[0] for letter and word[1] for value, or use flatten: true to get strings like 'G0'.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: parser.parseFile is not a function
Using ES module import syntax with require or missing installation.
fix
Use const parser = require('gcode-parser'); and run in Node.js CJS environment.
TypeError: Cannot read property '0' of undefined
Assuming words are always present in result, but empty lines return { words: [] }.
fix
Check for empty array before accessing elements: if (result.words.length > 0) { ... }
TypeError: callback is not a function
Calling parseFile without a second argument (callback) or passing a non-function.
fix
Always provide a callback: parser.parseFile('file.nc', (err, result) => { ... });
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
gcode-parser — npm install gcode-parser · libregistry