@fnando/cnpj

中文文档
2.0.0发布于 2024/10/28MITTypeScriptES Module零依赖近 30 天下载 7.9万

Validate, generate and format CNPJ numbers

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. Strict Validation

CNPJ

NPM package version License: MIT

This package does some 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

// 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:

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:

cnpj.isValid(number, strict);

The same example would now return false:

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

版本与发布记录

@fnando/cnpj 共发布 6 个版本,首次发布于 2018/04/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2024/10/282 年前
1.0.22020/07/226 年前
1.0.12020/02/206 年前
1.0.02020/02/206 年前
0.1.12018/10/278 年前
0.1.02018/04/048 年前

关于 @fnando/cnpj 的常见问题

@fnando/cnpj 是什么?

Validate, generate and format CNPJ numbers

如何安装 @fnando/cnpj?

在项目目录运行 npm install @fnando/cnpj,或使用 pnpm add / yarn add / bun add。

@fnando/cnpj 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fnando/cnpj 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fnando/cnpj 可以商用吗?

@fnando/cnpj 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fnando/cnpj 的下载量有多少?

根据 npm 官方下载统计,@fnando/cnpj 在 2026/10/15 – 2026/11/13 这 30 天内被下载 78,839 次,日均约 2,628 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fnando/cnpj 还在维护吗?

最新版本 2.0.0 发布于 2024/10/28(2 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/04/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。