Neutralinojs 是一个轻量、跨平台、基于 JavaScript + Web 技术开发桌面应用的框架。它不像 Electron 那样捆绑 Chromium,而是使用操作系统内置的 WebView 显示界面,同时通过 WebSocket 与原生服务通信实现本机 API 功能。


一. 概述与特点

Neutralinojs 是一款用于构建轻量级跨平台桌面应用的框架:

  • 使用 JavaScript/HTML/CSS 开发桌面应用。
  • 便携且体积小(vs Electron 极大优势)。
  • 支持 Linux / macOS / Windows / Web。
  • 原生功能 API 通过 WebSocket 实现。
  • 支持扩展开发与后台集成。

二. 环境准备 & 创建项目

接下来,我们来安装 Neu CLI 并创建一个新项目。让大家可以快速上手 Neutralinojs。

1.安装 Neu CLI

npm install -g @neutralinojs/neu

2.创建项目

neu create my-app
cd my-app

三. 项目结构解读

my-app/
├── resources/
│   └── neutralino.js     # 客户端 API 库
├── static/               # 应用前端资源
│   └── index.html
├── neutralino.config.json # 配置文件
├── neu.config.js         # CLI 配置
└── ...

四. 配置文件详解

neutralino.config.json 是 Neutralino 应用的核心配置,主要用于权限、服务模式等设置。

示例:

{
  "applicationId": "app.myapp",
  "defaultMode": "window",
  "nativeAllowList": [
    "app.*",
    "window.*",
    "os.execCommand"
  ],
  "nativeBlockList": [],
  "cli": {
    "resourcesPath": "/resources"
  }
}

nativeAllowList 控制允许调用哪些原生 API,安全性建议根据需调用的 API 配置而不是全开。


五. 启动与运行模式

1.开发模式

neu run

自动监听资源变更,自动重载。

2.编译 & 构建

neu build --release

自动生成跨平台可执行文件。


六. 原生 API 使用

1.初始化

在任何原生 API 调用前,必须初始化:

<script src="neutralino.js"></script>
<script>
  Neutralino.init();
</script>

或者:

import { init } from '@neutralinojs/lib';
await init();

初始化会建立 WebSocket 连接用于后续 API 调用。


6.2 窗口控制(Neutralino.window)

await Neutralino.window.maximize();
await Neutralino.window.restore();
await Neutralino.window.setTitle("我的 App");
await Neutralino.window.setSize(800, 600);
await Neutralino.window.setBorderless(true); // v6.x 新增 API :contentReference[oaicite:9]{index=9}

6.3 文件系统(Neutralino.filesystem)

await Neutralino.filesystem.writeFile("data.txt", "Hello 世界");

const content = await Neutralino.filesystem.readFile("data.txt");
console.log(content); // 原生 API 返回结果

文件监听:

const watcher = await Neutralino.filesystem.createWatcher("/path");
Neutralino.events.on("watchFile", (data) => {
  console.log(data); // 文件变化事件
});

文件监听功能是 v6 新增的原生能力之一。 ([neutralino.js.org][4])


6.4 操作系统命令(Neutralino.os.execCommand)

const res = await Neutralino.os.execCommand("ls -la");
console.log(res.stdOut);
console.log(res.stdErr);

6.5 系统信息(Neutralino.computer)

const arch = await Neutralino.computer.getArch();
const osInfo = await Neutralino.computer.getOSInfo();
console.log(arch, osInfo);

6.6 剪贴板(Neutralino.clipboard)

await Neutralino.clipboard.writeText("Hello from Native");
const text = await Neutralino.clipboard.readText();
console.log(text);

6.7 数据持久化(Neutralino.storage)

await Neutralino.storage.setData("user", {name:"Alice"});
const user = await Neutralino.storage.getData("user");
await Neutralino.storage.removeData("user");
await Neutralino.storage.clear();

6.8 事件机制(Neutralino.events)

监听事件:

Neutralino.events.on("ready", () => console.log("API Ready"));
Neutralino.events.on("windowClose", async () => {
  await Neutralino.app.exit();
});

6.9 扩展机制(Neutralino.extensions)

通过扩展 API 实现和其他程序或语言通信:

await Neutralino.extensions.dispatch("my.ext.id", "customEvent", {foo:42});

扩展可以接收返回消息处理逻辑。 ([neutralino.windhc.com][3])


七. 实用功能清单与示例

1.显示原生消息框

await Neutralino.os.showMessageBox("提示", "操作成功!");

2.弹出原生确认框

const yes = await Neutralino.os.showMessageBox({
  type: "question",
  buttons: ["Yes", "No"],
  title: "确认",
  message: "确定删除吗?"
});

3.文件选择器

const file = await Neutralino.os.showOpenDialog();
console.log(file);

4.打印内容

浏览器中常用 print:

await Neutralino.window.print();

八. 打包发布

生成 Windows、macOS、Linux 可执行文件:

neu build --release

将打包产物发布到 GitHub Releases、官网等渠道即可。


九. 总结

Neutralinojs 提供了一套轻量、跨平台、一致性的桌面开发体验。
用它你完全可以构建实用、可发布的现代桌面应用。

我还封装了一个 结合 vue3 ,支持热更新的开箱即用的。需要代码可以查看:
Electron太重?试试Neutralino

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐