你将如何从一个node.js命令行脚本启动浏览器
可能重复:
如何使用nodejs打开默认浏览器并导航到特定的URL
我不知道是否重要,但我在OSX上。
我知道你可以通过键入命令行来启动浏览器:
open http://www.stackoverflow.com
但有没有办法从nodejs命令行脚本中打开浏览器?
Opn现在存在,使用它。 🙂
安装时使用:
$ npm install --save opn
用于:
const opn = require('opn'); // Opens the image in the default image viewer opn('unicorn.png').then(() => { // image viewer closed }); // Opens the url in the default browser opn('http://sindresorhus.com'); // Specify the app to open in opn('http://sindresorhus.com', {app: 'firefox'}); // Specify app arguments opn('http://sindresorhus.com', {app: ['google chrome', '--incognito']});var open = require('open'); open('http://www.google.com'); You can also select a specific browser: open('http://www.google.com', 'firefox'); Or handle an error callback: open('http://www.google.com', function (err) { if (err) throw err; console.log('The user closed the browser'); });