# @protobufjs/codegen

> A minimalistic code generation utility.

Latest version **2.0.5** (published 2026-04-27) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install @protobufjs/codegen
pnpm add @protobufjs/codegen
yarn add @protobufjs/codegen
bun add @protobufjs/codegen
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high maintenance score; popular repo.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2026-04-27 |
| First published | 2016-12-11 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10586 |
| Author | Daniel Wirtz |
| Maintainers | dcode, protobufjs, google-wombot |

## Links

- npm: https://www.npmjs.com/package/@protobufjs/codegen
- Repository: https://github.com/dcodeIO/protobuf.js
- Homepage: https://github.com/dcodeIO/protobuf.js#readme
- Issues: https://github.com/dcodeIO/protobuf.js/issues
- npm.io page: https://npm.io/package/@protobufjs/codegen

## Recent versions

- 2.0.5 (latest) — 2026-04-27
- 2.0.4 — 2017-06-09
- 2.0.3 — 2017-04-23
- 2.0.2 — 2017-04-23
- 2.0.1 — 2017-04-23
- 2.0.0 — 2017-04-23
- 1.0.8 — 2017-01-16
- 1.0.7 — 2017-01-16
- 1.0.6 — 2017-01-12
- 1.0.5 — 2017-01-05
- 1.0.4 — 2017-01-03
- 1.0.3 — 2016-12-20
- 1.0.2 — 2016-12-12
- 1.0.1 — 2016-12-11
- 1.0.0 — 2016-12-11

## README

@protobufjs/codegen
===================
[![npm](https://img.shields.io/npm/v/@protobufjs/codegen.svg)](https://www.npmjs.com/package/@protobufjs/codegen)

A minimalistic code generation utility.

API
---

* **codegen([functionParams: `string[]`], [functionName: string]): `Codegen`**<br />
  Begins generating a function.

* **codegen.verbose = `false`**<br />
  When set to true, codegen will log generated code to console. Useful for debugging.

Invoking **codegen** returns an appender function that appends code to the function's body and returns itself:

* **Codegen(formatString: `string`, [...formatParams: `any`]): Codegen**<br />
  Appends code to the function's body. The format string can contain placeholders specifying the types of inserted format parameters:

  * `%d`: Number (integer or floating point value)
  * `%f`: Floating point value
  * `%i`: Integer value
  * `%j`: JSON.stringify'ed value
  * `%s`: String value
  * `%%`: Percent sign<br />

* **Codegen([scope: `Object.<string,*>`]): `Function`**<br />
  Finishes the function and returns it.

* **Codegen.toString([functionNameOverride: `string`]): `string`**<br />
  Returns the function as a string.

Example
-------

```js
var codegen = require("@protobufjs/codegen");

var add = codegen(["a", "b"], "add") // A function with parameters "a" and "b" named "add"
  ("// awesome comment")             // adds the line to the function's body
  ("return a + b - c + %d", 1)       // replaces %d with 1 and adds the line to the body
  ({ c: 1 });                        // adds "c" with a value of 1 to the function's scope

console.log(add.toString()); // function add(a, b) { return a + b - c + 1 }
console.log(add(1, 2));      // calculates 1 + 2 - 1 + 1 = 3
```

**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)

---
_Source: https://npm.io/package/@protobufjs/codegen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
