@napi-rs/wasm-runtime

中文文档
1.2.7发布于 2026/11/03MITESM + CJS构建来源已验证近 30 天下载 2.6亿

Runtime and polyfill for wasm targets

原始使用文档

@napi-rs/wasm-runtime

Runtime helpers and browser polyfills for napi-rs WebAssembly addons.

emnapi compatibility

Use matching versions of @emnapi/core and @emnapi/runtime:

  • v1: ^1.7.1. Core includes async work and threadsafe functions and ignores the plugins option. Loading this package does not resolve the v2-only @emnapi/core/plugins export.
  • v2: the peer range is exactly 2.0.0-beta.2 or 2.0.0-beta.3, and this package bundles the async-work and TSFN plugins from 2.0.0-beta.3. They depend on private core context fields, which are the same in both betas (2.0.0-beta.3 changed only the TSFN release order). 2.0.0-beta.1 renamed the context field napiModule to addonModule; the bundled plugins accept both names, so bindings that pin a 2.0.0 alpha keep loading. A later prerelease that changes the context again needs a new release of this package.

The v2 peer range stays narrow on purpose. With an open range, npm can hoist a binding's 2.0.0 alpha core next to this package, and then v1 bindings that load through the ESM entry, or through tools that skip the CommonJS copy lookup below, get a v2 core. With the narrow range, npm installs a v1 core for this package when a binding pins another v2 prerelease. The binding's own emnapi copy still runs it, as described below.

The CommonJS entry keeps emnapi copies apart when this package's peers resolve to a different copy than a binding's own @emnapi/core and @emnapi/runtime (two bindings pinning different emnapi versions, say):

  • instantiateNapiModule(Sync) runs the core of the copy whose runtime created options.context. It finds that runtime in require.cache and resolves the core from the module that required it (the binding's loader).
  • getDefaultContext, createContext, MessageHandler, and instantiation without a context use the copy of the calling package when it declares both emnapi packages as dependencies: the CommonJS module that is still loading and required this package, or, in a worker thread, the worker's entry file.
  • When the selected core is v2, the bundled plugins are replaced by that core's own @emnapi/core/plugins.
  • Anything else uses this package's peers. A context of the other emnapi generation from no known copy throws a TypeError that says so.

The ESM entry is resolved by bundlers and cannot do this. It uses the bundled plugins with the addonModule alias.

For raw instantiateNapiModule / instantiateNapiModuleSync calls loading a single-threaded napi-rs addon, pass plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin] from this package. Generated napi-rs loaders already supply these plugins.

Updating the bundled plugins

Update the exact v2 development dependencies together with the workspace resolutions. build checks that the resolved core version is the exact development version and one of the v2 versions in the peer range, and prepack rebuilds the bundles so a package cannot silently reuse output from an earlier build. Before a release, compare the new plugin context with the oldest supported alpha (2.0.0-alpha.3 is the oldest still pinned by published bindings), and keep the CI matrix covering it. Keep earlier v2 versions in the peer range while published bindings pin them (rolldown 1.2.16 pins 2.0.0-beta.2); removing one makes npm install a v1 core for this package next to those bindings.

yarn workspace @napi-rs/wasm-runtime test exercises actual WebAssembly initialization, async work, TSFN calls from a worker, worker load failures, and cleanup through both module formats. CI also installs the packed tarball with real v1 and v2 peers outside the workspace to avoid root resolution overrides.

版本与发布记录

@napi-rs/wasm-runtime 共发布 39 个版本,首次发布于 2024/01/17;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.7latest2026/11/032 天前
1.2.62026/10/2511 天前
1.2.52026/10/051 个月前
1.2.42026/09/101 个月前
1.2.32026/08/122 个月前
1.2.22026/07/313 个月前

关于 @napi-rs/wasm-runtime 的常见问题

@napi-rs/wasm-runtime 是什么?

Runtime and polyfill for wasm targets

如何安装 @napi-rs/wasm-runtime?

在项目目录运行 npm install @napi-rs/wasm-runtime,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@emnapi/core(^1.7.1 || 2.0.0-beta.2 || 2.0.0-beta.3)、@emnapi/runtime(^1.7.1 || 2.0.0-beta.2 || 2.0.0-beta.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@napi-rs/wasm-runtime 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/napi-rs__wasm-runtime 是否存在。

@napi-rs/wasm-runtime 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@napi-rs/wasm-runtime 需要什么版本的 Node.js?

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

@napi-rs/wasm-runtime 可以商用吗?

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

@napi-rs/wasm-runtime 的下载量有多少?

根据 npm 官方下载统计,@napi-rs/wasm-runtime 在 2026/10/05 – 2026/11/03 这 30 天内被下载 255,147,006 次,日均约 8,504,900 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@napi-rs/wasm-runtime 还在维护吗?

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