Prototxt Parser
prototxt-parser is a JavaScript and TypeScript library designed to parse `.prototxt` files, commonly used for defining neural network architectures in frameworks like Caffe, into standard JavaScript objects. It is built upon the `parsimmon` parser combinator library, which provides a flexible and robust foundation for defining custom grammars. The current stable version is 0.1.5. Given its low version number and specialized use case, its release cadence is likely slow and driven by specific needs rather than a regular schedule. Key differentiators include its explicit focus on `.prototxt` syntax, providing a direct mapping to JavaScript objects, and shipping with TypeScript type definitions for enhanced developer experience. It offers a straightforward API for both browser and Node.js environments, allowing developers to integrate `.prototxt` parsing into various JavaScript-based applications. While it doesn't aim to be a general-purpose configuration parser, its dedicated approach makes it suitable for working with Caffe-related model definitions.
Common errors
-
TypeError: prototxtParser.parse is not a function
cause The `parse` function is accessed directly as a named export, but it's actually a method of the default (or namespace) export `prototxtParser`.fixEnsure you are calling it as `prototxtParser.parse(yourString)`. If using ESM, make sure to `import * as prototxtParser from 'prototxt-parser';` or for CJS, `const prototxtParser = require('prototxt-parser');`. -
Module not found: Can't resolve 'prototxt-parser'
cause The package `prototxt-parser` has not been installed or is not correctly listed in your project's dependencies, or there's a module resolution issue in your bundler/runtime.fixRun `npm install prototxt-parser` or `yarn add prototxt-parser` to add it to your project. Verify your bundler (e.g., Webpack, Rollup) or Node.js environment is configured to resolve modules from `node_modules`. -
Error: Expected a Protobuf field assignment (like 'key: value' or 'key { ... }') but got something elsecause The input string is not a valid `.prototxt` format, or contains syntax that the `parsimmon`-based parser does not expect (e.g., missing colons, malformed blocks, unexpected characters).fixCarefully review the input string against the `.prototxt` syntax rules, especially for Caffe. Use a `.prototxt` linter if available, or compare against known working examples. The error message from `parsimmon` is usually quite specific about the unexpected token or position.
Warnings
- gotcha The library is specifically designed for `.prototxt` format as used by Caffe. It may not fully support all variations or advanced features of Google's Protocol Buffers text format, which can be more general. If your `.prototxt` deviates significantly from Caffe's conventions, parsing might fail or produce unexpected results.
- gotcha The development activity on the GitHub repository has been minimal since its last update approximately 4 years ago (as of 2026), and the latest release is version 0.1.5. While functional for its intended purpose, expect slow or non-existent updates for new features, bug fixes, or compatibility with newer JavaScript environments or parsing standards.
- gotcha The `rawgit.com` CDN mentioned in the original README for browser usage has been deprecated and is no longer actively maintained since 2018. Relying on such deprecated services poses a security risk and can lead to broken deployments.
Install
-
npm install prototxt-parser -
yarn add prototxt-parser -
pnpm add prototxt-parser
Imports
- prototxtParser
import prototxtParser from 'prototxt-parser'; // Not a default export import { parse } from 'prototxt-parser'; // The main 'parse' function is a member of the 'prototxtParser' objectimport * as prototxtParser from 'prototxt-parser';
- prototxtParser
const prototxtParser = require('prototxt-parser'); - prototxtParser.parse
parse(prototxtString); // 'parse' is not a top-level export
prototxtParser.parse(prototxtString);
Quickstart
import * as prototxtParser from 'prototxt-parser';
async function fetchPrototxt(uri: string): Promise<string> {
try {
const response = await fetch(new Request(uri));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.text();
} catch (error) {
console.error(`Failed to fetch prototxt from ${uri}:`, error);
throw error;
}
}
// Example .prototxt for a SqueezeNet model
const uri = "https://raw.githubusercontent.com/DeepScale/SqueezeNet/master/SqueezeNet_v1.1/deploy.prototxt";
(async () => {
try {
const prototxtString = await fetchPrototxt(uri);
const parsedProto = prototxtParser.parse(prototxtString);
console.log('Successfully parsed .prototxt:');
console.log(JSON.stringify(parsedProto, null, 2));
// You can now access properties, e.g., console.log(parsedProto.name);
} catch (error) {
console.error('An error occurred during parsing:', error);
}
})();