# @fnando/cnpj

> Validate, generate and format CNPJ numbers

Latest version **2.0.0** (published 2024-10-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fnando/cnpj
pnpm add @fnando/cnpj
yarn add @fnando/cnpj
bun add @fnando/cnpj
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-10-27 |
| First published | 2018-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 90 |
| Author | Nando Vieira |
| Maintainers | fnando |
| Keywords | cnpj, document |

## Links

- npm: https://www.npmjs.com/package/@fnando/cnpj
- Repository: https://github.com/fnando/cnpj
- Homepage: https://github.com/fnando/cnpj#readme
- Issues: https://github.com/fnando/cnpj/issues
- npm.io page: https://npm.io/package/@fnando/cnpj

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2024-10-27
- 1.0.2 — 2020-07-21
- 1.0.1 — 2020-02-19
- 1.0.0 — 2020-02-19
- 0.1.1 — 2018-10-26
- 0.1.0 — 2018-04-04

## README

# CNPJ

[![NPM package version](https://img.shields.io/npm/v/@fnando/cnpj.svg)](https://www.npmjs.com/package/@fnando/cnpj)
![License: MIT](https://img.shields.io/npm/l/@fnando/cnpj.svg)

This package does some [CNPJ](http://en.wikipedia.org/wiki/CNPJ) magic. It
allows you to create, validate and format CNPJ documents.

> [!NOTE]
>
> This library already supports the new alphanumeric CNPJ algorithm that will be
> available starting July 2026. For more information, see
> <https://www.gov.br/receitafederal/pt-br/acesso-a-informacao/acoes-e-programas/programas-e-atividades/cnpj-alfanumerico>.

**HINT:** Check out the CPF counter part available at
<https://github.com/fnando/cpf>.

## Installation

This lib is available as a NPM package. To install it, use the following
command:

```
npm install @fnando/cnpj --save
```

If you're using Yarn (and you should):

```
yarn add @fnando/cnpj
```

## Usage

```js
// Node.js-specific
const cnpj = require("@fnando/cnpj/commonjs");

// @import
import * as cnpj from "@fnando/cnpj"; // import the whole library
import { isValid as isValidCnpj } from "@fnando/cnpj"; // import just one function

// import via <script>; the lib will available as window.CNPJ
// <script src="cnpj.js"></script>

cnpj.isValid("41.381.074/6738-65");
//=> true

cnpj.isValid("41381074673865");
//=> true

cnpj.strip("41.381.074/6738-65");
//=> 41381074673865

cnpj.format("41381074673865");
//=> 41.381.074/6738-65

cnpj.generate(true); // generate formatted number
//=> 54.385.406/3140-07

cnpj.generate(); // generate unformatted number
//=> 07033324230766
```

On the web, without transformation, just use `web/cnpj.min.js`.

### Strict Validation

By default, validations will strip any characters you provide. This means that
the following is valid, because only numbers will be considered:

```js
cnpj.isValid("41#381#074-----6738\n\n65");
//=> true

cnpj.strip("41#381#074-----6738\n\n65");
//=> 41381074673865
```

If you want to strict validate strings, use the following signature:

```js
cnpj.isValid(number, strict);
```

The same example would now return `false`:

```js
cnpj.isValid("41#381#074-----6738\n\n65", true);
//=> false
```

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