@circleback/node-swift

中文文档
1.4.1发布于 2026/01/30MITTypeScriptCommonJS近 30 天下载 3980

Support for creating Node modules in Swift

原始使用文档
文档目录6 节
  1. What is it?
  2. Example
  3. Features
  4. How?
  5. Get started
  6. Alternatives

NodeSwift

Bridge Node.js and Swift code.

What is it?

NodeSwift allows you to write Swift code that talks to Node.js libraries, and vice versa. This enables possibilities such as

  • Using native macOS APIs and SwiftPM in an Electron app.
  • Interacting with the vast array of NPM APIs from a Swift program (e.g. a macOS app, iOS app, or a Vapor server).
  • Speeding up your JS code by writing performance critical bits in Swift.

Example

MyModule.swift

import NodeAPI

#NodeModule(exports: [
    "nums": [Double.pi.rounded(.down), Double.pi.rounded(.up)],
    "str": String(repeating: "NodeSwift! ", count: 3),
    "add": try NodeFunction { (a: Double, b: Double) in
        "\(a) + \(b) = \(a + b)"
    },
])

index.js

const { nums, str, add } = require("./build/MyModule.node");
console.log(nums); // [ 3, 4 ]
console.log(str); // NodeSwift! NodeSwift! NodeSwift!
console.log(add(5, 10)); // 5.0 + 10.0 = 15.0

Features

  • Safe: NodeSwift makes use of Swift's memory safety and automatic reference counting. This means that, unlike with the C-based Node-API, you never have to think about memory management while writing NodeSwift modules.
  • Simple: With progressive disclosure, you can decide whether you want to use simpler or more advanced NodeSwift APIs to suit whatever your needs might be.
  • Idiomatic: NodeSwift's APIs feel right at home in idiomatic Swift code. For example, to make a Swift class usable from Node.js you literally declare a class in Swift that conforms to NodeClass. We also use several Swift features like Dynamic Member Lookup that are designed precisely to make this sort of interop easy.
  • Versatile: You have access to the full set of Node.js APIs in Swift, from JavaScript object manipulation to event loop scheduling.
  • Cross-platform: NodeSwift works not only on macOS, but also on Linux, Windows, and even iOS!

How?

A NodeSwift module consists of an SwiftPM package and NPM package in the same folder, both of which express NodeSwift as a dependency.

The Swift package is exposed to JavaScript as a native Node.js module, which can be require'd by the JS code. The two sides communicate via Node-API, which is wrapped by the NodeAPI module on the Swift side.

Get started

For details, see the example in /example.

Alternatives

WebAssembly

While WebAssembly is great for performance, it still runs in a virtual machine, which means it can't access native Darwin/Win32/GNU+Linux APIs. NodeSwift runs your Swift code on the bare metal, which should be even faster than WASM, in addition to unlocking access to the operating system's native APIs.

On the other hand, if you want to run Swift code in the browser, WebAssembly might be the right choice since NodeSwift requires a Node.js runtime.

Other NAPI wrappers

NAPI, NAN, Neon etc. are all other options for building native Node.js modules, each with its own strengths. For example, NAPI is written in C and thus affords great portability at the cost of memory unsafety. NodeSwift is a great option if you want to enhance your JS tool on Apple platforms, if you want to bring Node.js code into your existing Swift program, or if you simply prefer Swift to C/C++/Rust/etc.

版本与发布记录

@circleback/node-swift 共发布 2 个版本,首次发布于 2026/01/30;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.4.1latest2026/01/309 个月前
1.4.02026/01/309 个月前

关于 @circleback/node-swift 的常见问题

@circleback/node-swift 是什么?

Support for creating Node modules in Swift

如何安装 @circleback/node-swift?

在项目目录运行 npm install @circleback/node-swift,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 node-swift,可以用 npx node-swift 直接运行。

@circleback/node-swift 支持 TypeScript 吗?

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

@circleback/node-swift 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@circleback/node-swift 可以商用吗?

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

@circleback/node-swift 的下载量有多少?

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

@circleback/node-swift 还在维护吗?

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