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.

90 lines
2.3 KiB

4 years ago
  1. # opn
  2. > A better [node-open](https://github.com/pwnall/node-open). Opens stuff like websites, files, executables. Cross-platform.
  3. If need this for Electron, use [`shell.openItem()`](https://electronjs.org/docs/api/shell#shellopenitemfullpath) instead.
  4. #### Why?
  5. - Actively maintained
  6. - Supports app arguments
  7. - Safer as it uses `spawn` instead of `exec`
  8. - Fixes most of the open `node-open` issues
  9. - Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux
  10. ## Install
  11. ```
  12. $ npm install opn
  13. ```
  14. ## Usage
  15. ```js
  16. const opn = require('opn');
  17. // Opens the image in the default image viewer
  18. opn('unicorn.png').then(() => {
  19. // image viewer closed
  20. });
  21. // Opens the url in the default browser
  22. opn('http://sindresorhus.com');
  23. // Specify the app to open in
  24. opn('http://sindresorhus.com', {app: 'firefox'});
  25. // Specify app arguments
  26. opn('http://sindresorhus.com', {app: ['google chrome', '--incognito']});
  27. ```
  28. ## API
  29. Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
  30. ### opn(target, [options])
  31. 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.
  32. #### target
  33. Type: `string`
  34. The thing you want to open. Can be a URL, file, or executable.
  35. Opens in the default app for the file type. For example, URLs opens in your default browser.
  36. #### options
  37. Type: `Object`
  38. ##### wait
  39. Type: `boolean`<br>
  40. Default: `true`
  41. Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
  42. On Windows you have to explicitly specify an app for it to be able to wait.
  43. ##### app
  44. Type: `string` `Array`
  45. Specify the app to open the `target` with, or an array with the app and app arguments.
  46. 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.
  47. ## Related
  48. - [opn-cli](https://github.com/sindresorhus/opn-cli) - CLI for this module
  49. ## License
  50. MIT © [Sindre Sorhus](https://sindresorhus.com)