nw.js打开双应用:"single-instance":false弃用后的实现方法
Hey there! Let's tackle your NW.js questions one by one, nice and clear:
1. 如何使用NW.js打开2个不同的应用
This is actually straightforward—if your two apps are separate (each has its own package.json and resources), you just need to launch them independently:
- Open a terminal, navigate to the first app's root folder, and run
nw .(make sure NW.js is in your system PATH, or use the full path to the NW executable if it's not). - Open another terminal window, head to the second app's root folder, and run
nw .again.
Boom, two separate NW.js apps will run side by side, completely independent of each other.
2. 当single-instance配置项被弃用时,如何启动同一个NW.js应用的多个实例
First, a quick clarification: the single-instance manifest option is indeed deprecated in NW.js. If your goal is to run multiple copies of the same app (not different apps), here are two solid workarounds:
方法1:启动时添加--multi-instance命令行参数
NW.js provides a command-line flag to force multi-instance behavior. Just add it when launching your app:
nw . --multi-instance
Run the same command again to spin up a second instance of the app. This flag overrides the default single-instance restriction.
方法2:在应用内部通过代码触发新实例
If you want to open a new instance from within the running app (like via a button click), use NW.js's App module to launch a new process:
const { App } = require('nw.gui'); const path = require('path'); // Example function to trigger when a button is clicked function openNewAppInstance() { const appRoot = process.cwd(); // Launch a new instance with the --multi-instance flag App.openFile(path.join(appRoot, 'package.json'), { args: ['--multi-instance'] }); }
This lets you spawn a new copy of the same app directly from the existing one.
小补充
If you still just need to open different apps, you don't have to worry about the deprecated single-instance setting at all. NW.js treats apps with different name or uuid values (configured in package.json) as separate entities, so launching them individually works by default.
内容的提问来源于stack exchange,提问作者Eduard Arevshatyan

