You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.5 KiB

1 month ago
  1. # open
  2. > Open stuff like URLs, files, executables. Cross-platform.
  3. This is meant to be used in command-line tools and scripts, not in the browser.
  4. If you need this for Electron, use [`shell.openPath()`](https://www.electronjs.org/docs/api/shell#shellopenpathpath) instead.
  5. This package does not make any security guarantees. If you pass in untrusted input, it's up to you to properly sanitize it.
  6. #### Why?
  7. - Actively maintained.
  8. - Supports app arguments.
  9. - Safer as it uses `spawn` instead of `exec`.
  10. - Fixes most of the original `node-open` issues.
  11. - Includes the latest [`xdg-open` script](https://gitlab.freedesktop.org/xdg/xdg-utils/-/blob/master/scripts/xdg-open.in) for Linux.
  12. - Supports WSL paths to Windows apps.
  13. ## Install
  14. ```sh
  15. npm install open
  16. ```
  17. ## Usage
  18. ```js
  19. const open = require('open');
  20. // Opens the image in the default image viewer and waits for the opened app to quit.
  21. await open('unicorn.png', {wait: true});
  22. console.log('The image viewer app quit');
  23. // Opens the URL in the default browser.
  24. await open('https://sindresorhus.com');
  25. // Opens the URL in a specified browser.
  26. await open('https://sindresorhus.com', {app: {name: 'firefox'}});
  27. // Specify app arguments.
  28. await open('https://sindresorhus.com', {app: {name: 'google chrome', arguments: ['--incognito']}});
  29. // Open an app
  30. await open.openApp('xcode');
  31. // Open an app with arguments
  32. await open.openApp(open.apps.chrome, {arguments: ['--incognito']});
  33. ```
  34. ## API
  35. It uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
  36. ### open(target, options?)
  37. Returns a promise for the [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
  38. #### target
  39. Type: `string`
  40. The thing you want to open. Can be a URL, file, or executable.
  41. Opens in the default app for the file type. For example, URLs opens in your default browser.
  42. #### options
  43. Type: `object`
  44. ##### wait
  45. Type: `boolean`\
  46. Default: `false`
  47. Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
  48. Note that it waits for the app to exit, not just for the window to close.
  49. On Windows, you have to explicitly specify an app for it to be able to wait.
  50. ##### background <sup>(macOS only)</sup>
  51. Type: `boolean`\
  52. Default: `false`
  53. Do not bring the app to the foreground.
  54. ##### newInstance <sup>(macOS only)</sup>
  55. Type: `boolean`\
  56. Default: `false`
  57. Open a new instance of the app even it's already running.
  58. A new instance is always opened on other platforms.
  59. ##### app
  60. Type: `{name: string | string[], arguments?: string[]} | Array<{name: string | string[], arguments: string[]}>`
  61. Specify the `name` of the app to open the `target` with, and optionally, app `arguments`. `app` can be an array of apps to try to open and `name` can be an array of app names to try. If each app fails, the last error will be thrown.
  62. The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`open.apps`](#openapps) which auto-detects the correct binary to use.
  63. You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.
  64. The app `arguments` are app dependent. Check the app's documentation for what arguments it accepts.
  65. ##### allowNonzeroExitCode
  66. Type: `boolean`\
  67. Default: `false`
  68. Allow the opened app to exit with nonzero exit code when the `wait` option is `true`.
  69. We do not recommend setting this option. The convention for success is exit code zero.
  70. ### open.apps
  71. An object containing auto-detected binary names for common apps. Useful to work around [cross-platform differences](#app).
  72. ```js
  73. const open = require('open');
  74. await open('https://google.com', {
  75. app: {
  76. name: open.apps.chrome
  77. }
  78. });
  79. ```
  80. #### Supported apps
  81. - [`chrome`](https://www.google.com/chrome) - Web browser
  82. - [`firefox`](https://www.mozilla.org/firefox) - Web browser
  83. - [`edge`](https://www.microsoft.com/edge) - Web browser
  84. ### open.openApp(name, options?)
  85. Open an app.
  86. Returns a promise for the [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
  87. #### name
  88. Type: `string`
  89. The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`open.apps`](#openapps) which auto-detects the correct binary to use.
  90. You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.
  91. #### options
  92. Type: `object`
  93. Same options as [`open`](#options) except `app` and with the following additions:
  94. ##### arguments
  95. Type: `string[]`\
  96. Default: `[]`
  97. Arguments passed to the app.
  98. These arguments are app dependent. Check the app's documentation for what arguments it accepts.
  99. ## Related
  100. - [open-cli](https://github.com/sindresorhus/open-cli) - CLI for this module
  101. - [open-editor](https://github.com/sindresorhus/open-editor) - Open files in your editor at a specific line and column