@ensdomains/ensjs

中文文档
5.3.1发布于 2026/11/13MITTypeScriptES Module构建来源已验证近 30 天下载 11.3万

ENS javascript library for contract interaction

原始使用文档
文档目录10 节
  1. Features
  2. Monorepo layout
  3. Installation
  4. Getting started
  5. Entry points
  6. Action pattern
  7. Development
  8. Contributing
  9. Docs
  10. License

ENSjs

The ultimate ENS JavaScript library, with viem under the hood.

ENSjs is a TypeScript library for interacting with the Ethereum Name Service. It provides tree-shakeable, composable actions on top of viem, with first-class support for both ENS v1 and ENS v2.

Features

  • Composable actions that extend any viem Client
  • Full tree-shaking — pay only for the actions you import
  • Multicall batching for read actions
  • TypeScript-first, with strict types for chain contracts
  • ENS v1 + ENS v2 support side by side
  • Subgraph and DNS helpers included
  • Standalone @ensdomains/ensjs-abi package for ABI snippets

Monorepo layout

PackageDescription
@ensdomains/ensjsMain library — actions, clients, utils
@ensdomains/ensjs-abiABI snippets for every ENS contract (v1 + v2)
@ensdomains/ensjs-reactReact hooks built on @wagmi/core
@ensdomains/ensjs-query-core@wagmi/core query integration

Supported chains: mainnet (1) and sepolia (11155111).

Installation

pnpm add @ensdomains/ensjs viem

You'll also need viem ≥ 2.9.2 as a peer dependency. Some features need optional peer dependencies:

PackageNeeded for
graphql@ensdomains/ensjs/subgraph
@ensdomains/dnsprovejs (^0.5.4 || ^1.0.0)getDnsImportData from @ensdomains/ensjs/dns

Getting started

addEnsContracts extends a viem chain with all ENS contract addresses and subgraph URLs, so you can use it with any viem createPublicClient / createWalletClient.

import { http, createPublicClient } from 'viem'
import { mainnet } from 'viem/chains'
import { addEnsContracts } from '@ensdomains/ensjs'
import { getAddressRecord, getRecords } from '@ensdomains/ensjs/public'

const client = createPublicClient({
  chain: addEnsContracts(mainnet),
  transport: http(),
})

const eth = await getAddressRecord(client, { name: 'ens.eth' })
const records = await getRecords(client, {
  name: 'ens.eth',
  texts: ['com.twitter', 'avatar'],
  contentHash: true,
})

Entry points

The @ensdomains/ensjs package is split into subpath exports so bundlers can drop everything you don't import.

ImportContents
@ensdomains/ensjsaddEnsContracts, error classes
@ensdomains/ensjs/publicShared read actions (resolution, records, reverse, price, availability)
@ensdomains/ensjs/public/v1v1-specific reads
@ensdomains/ensjs/public/v2v2-specific reads
@ensdomains/ensjs/walletShared write actions (commit, register, renew, set individual records)
@ensdomains/ensjs/wallet/v1v1-specific writes (wrap/unwrap, fuses, subnames, transfer, setRecords, primary name)
@ensdomains/ensjs/wallet/v2v2-specific writes
@ensdomains/ensjs/subgraphSubgraph client + queries (getSubnames, getNamesForAddress, history, …)
@ensdomains/ensjs/dnsDNS helpers (getDnsOwner, importDnsName, getDnsImportData, …)
@ensdomains/ensjs/utilsCoders (getAddress, getText, getAbi, getContentHash), name utils
@ensdomains/ensjs/utils/v2v2 utils (role encoding, resolver resources, canonical IDs)
@ensdomains/ensjs/contractsRe-exports of ABI snippets
@ensdomains/ensjs/chainChain types and helpers (ChainWithEns, extendChainWithEns, getChainContractAddress)

ABIs themselves are published as a standalone package and can be imported directly:

import { permissionedRegistryGetStateSnippet } from '@ensdomains/ensjs-abi/v2/permissionedRegistry'
import { ethRegistrarControllerRegisterSnippet } from '@ensdomains/ensjs-abi/v1/ethRegistrarController'

Action pattern

Every action is a plain function that takes the viem Client first and parameters second.

const result = await actionName(client, {
  /* params */
})

Read actions use getAction + readContract / multicall so they work with whatever batching configuration is on the client. Write actions return a transaction hash and accept the standard viem write parameters (account, chain, gas, etc.).

Development

# Install
pnpm install

# Build everything
pnpm -r build

# Build a single package
pnpm -F @ensdomains/ensjs build
pnpm -F @ensdomains/ensjs-abi build

# Lint (Biome)
pnpm lint

# Type check
pnpm -F @ensdomains/ensjs check:types

# Test the main package
pnpm -F @ensdomains/ensjs test
pnpm -F @ensdomains/ensjs test:watch
pnpm -F @ensdomains/ensjs test src/actions/public/resolver/getRecords.test.ts

# Generate the markdown docs
pnpm -F @ensdomains/ensjs generateDocs

# Versioning (changesets)
pnpm chgset:run
pnpm chgset:version

Tests run against a local devnet with the ENS contracts deployed and test names seeded (ghcr.io/ensdomains/contracts-v2), so Docker needs to be running. test starts the devnet via packages/ensjs/compose.yml and stops it afterwards. If one is already running on localhost:8545, it's reused:

docker compose -f packages/ensjs/compose.yml up -d devnet

Node ≥ 22 is required for the main packages (≥ 18 for query-core). Tooling: pnpm 12, TypeScript strict mode, Biome for formatting and linting, Vitest for tests.

Contributing

See CONTRIBUTING.md for guidance on adding new actions, wiring up new ABI snippets, registering new contract addresses, and the test conventions used across the repo.

Docs

Guides and the API reference live under docs/. The API reference is generated from the source JSDoc with pnpm -F @ensdomains/ensjs generateDocs.

License

MIT

版本与发布记录

@ensdomains/ensjs 共发布 143 个版本,首次发布于 2020/10/05;过去 12 个月发布了 26 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.3.1latest2026/11/13今天
5.3.02026/11/13今天
5.2.122026/11/0211 天前
5.2.112026/11/0211 天前
5.2.102026/10/2915 天前
5.2.92026/10/2816 天前

其他发布通道:rc-branch → 0.0.0-feat-new-dnssec-oracle-updated.20240123T214835358,alpha → 5.0.0-alpha.1,next → 4.2.0,sepolia-fix → 5.0.0-sepolia-fix.1。

关于 @ensdomains/ensjs 的常见问题

@ensdomains/ensjs 是什么?

ENS javascript library for contract interaction

如何安装 @ensdomains/ensjs?

在项目目录运行 npm install @ensdomains/ensjs,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:viem(^2.9.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ensdomains/ensjs 支持 TypeScript 吗?

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

@ensdomains/ensjs 支持 ESM 还是 CommonJS?

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

@ensdomains/ensjs 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ensdomains/ensjs 可以商用吗?

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

@ensdomains/ensjs 的下载量有多少?

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

@ensdomains/ensjs 还在维护吗?

最新版本 5.3.1 发布于 2026/11/13(今天);至今共发布 143 个版本,其中过去 12 个月发布了 26 个;首次发布于 2020/10/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。