您将如何从node.js命令行脚本启动浏览器

Bob*_*ian 50 shell command-line node.js

可能重复:
如何使用nodejs打开默认浏览器并导航到特定URL

我不知道这是否重要,但我在OSX上.

我知道您可以通过键入以下内容从命令行启动浏览器:

open http://www.stackoverflow.com
Run Code Online (Sandbox Code Playgroud)

但有没有办法从nodejs命令行脚本中打开浏览器?

cti*_*ide 99

Opn现在存在,使用它.:)

安装时间:

$ npm install --save open
Run Code Online (Sandbox Code Playgroud)

用于:

const open = require('open');

// Opens the image in the default image viewer
(async () => {
    await open('unicorn.png', {wait: true});
    console.log('The image viewer app closed');

    // Opens the url in the default browser
    await open('https://sindresorhus.com');

    // Specify the app to open in
    await open('https://sindresorhus.com', {app: 'firefox'});

    // Specify app arguments
    await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
})();
Run Code Online (Sandbox Code Playgroud)

  • https://github.com/fixedset/open.js处理x平台问题 (10认同)
  • 那么linux和windows怎么样? (5认同)
  • 不推荐使用节点打开.请更新到https://github.com/sindresorhus/opn. (5认同)
  • `opn`(不是拼写错误,它不包含'e')似乎是这些日子里使用的新人.https://www.npmjs.com/package/opn (4认同)
  • 我相信xdg-open是open for Linux的标准版本,但我不知道你用于Windows的是什么. (2认同)