39370d2898a4a58a994fac6e378f09f8d316e6624b6bc0c895dbbfa7b6549a01b16771ed593db315111d71cfc424a7f2bb97b4ae6dfb17542eca464c47e5f4 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # open
  2. > Open stuff like URLs, files, executables. Cross-platform.
  3. If need this for Electron, use [`shell.openItem()`](https://electronjs.org/docs/api/shell#shellopenitemfullpath) instead.
  4. Note: The original [`open` package](https://github.com/pwnall/node-open) was recently deprecated in favor of this package, and we got the name, so this package is now named `open` instead of `opn`. If you're upgrading from the original `open` package (`open@0.0.5` or lower), keep in mind that the API is different.
  5. #### Why?
  6. - Actively maintained.
  7. - Supports app arguments.
  8. - Safer as it uses `spawn` instead of `exec`.
  9. - Fixes most of the open original `node-open` issues.
  10. - Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux.
  11. - Supports WSL paths to Windows apps under `/mnt/*`.
  12. ## Install
  13. ```
  14. $ npm install open
  15. ```
  16. ## Usage
  17. ```js
  18. const open = require('open');
  19. (async () => {
  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: 'firefox'});
  27. // Specify app arguments
  28. await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
  29. })();
  30. ```
  31. ## API
  32. It uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
  33. ### open(target, options?)
  34. 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.
  35. #### target
  36. Type: `string`
  37. The thing you want to open. Can be a URL, file, or executable.
  38. Opens in the default app for the file type. For example, URLs opens in your default browser.
  39. #### options
  40. Type: `object`
  41. ##### wait
  42. Type: `boolean`<br>
  43. Default: `false`
  44. Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
  45. Note that it waits for the app to exit, not just for the window to close.
  46. On Windows, you have to explicitly specify an app for it to be able to wait.
  47. ##### background <sup>(macOS only)</sup>
  48. Type: `boolean`<br>
  49. Default: `false`
  50. Do not bring the app to the foreground.
  51. ##### app
  52. Type: `string | string[]`
  53. Specify the app to open the `target` with, or an array with the app and app arguments.
  54. 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.
  55. 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.
  56. ## Related
  57. - [open-cli](https://github.com/sindresorhus/open-cli) - CLI for this module
  58. - [open-editor](https://github.com/sindresorhus/open-editor) - Open files in your editor at a specific line and column
  59. ---
  60. <div align="center">
  61. <b>
  62. <a href="https://tidelift.com/subscription/pkg/npm-opn?utm_source=npm-opn&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  63. </b>
  64. <br>
  65. <sub>
  66. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  67. </sub>
  68. </div>