node.js コマンドラインスクリプトからブラウザを起動するにはどうすればいいですか? [重複] 質問する

node.js コマンドラインスクリプトからブラウザを起動するにはどうすればいいですか? [重複] 質問する

関係があるかどうかは分かりませんが、私は OSX を使用しています。

次のように入力すると、コマンドラインからブラウザを起動できることは知っています。

open http://www.stackoverflow.com

しかし、Node.js コマンドライン スクリプト内からブラウザーを開く方法はありますか?

ベストアンサー1

開ける今は存在するので、それを使用してください。 :)

インストール:

$ npm install --save open

一緒に使用:

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']});
})();

オプションapp: ...

Type: string | string[]

ターゲットを開くアプリ、またはアプリとアプリ引数を含む配列を指定します。

アプリ名はプラットフォームに依存します。再利用可能なモジュールにハードコードしないでください。たとえば、Chrome は macOS では google chrome、Linux では google-chrome、Windows では chrome になります。

アプリのフルパスを渡すこともできます。たとえば、WSL では、Chrome の Windows インストールの場合、これは /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe になります。

例:

open('http://localhost', {app: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"});

おすすめ記事