UNPKG

3.14 kBMarkdownView Raw
1# open
2
3> Open stuff like URLs, files, executables. Cross-platform.
4
5If need this for Electron, use [`shell.openItem()`](https://electronjs.org/docs/api/shell#shellopenitemfullpath) instead.
6
7Note: 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.
8
9#### Why?
10
11- Actively maintained.
12- Supports app arguments.
13- Safer as it uses `spawn` instead of `exec`.
14- Fixes most of the open original `node-open` issues.
15- Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux.
16- Supports WSL paths to Windows apps under `/mnt/*`.
17
18
19## Install
20
21```
22$ npm install open
23```
24
25
26## Usage
27
28```js
29const open = require('open');
30
31// Opens the image in the default image viewer
32(async () => {
33 await open('unicorn.png', {wait: true});
34 console.log('The image viewer app closed');
35
36 // Opens the url in the default browser
37 await open('https://sindresorhus.com');
38
39 // Specify the app to open in
40 await open('https://sindresorhus.com', {app: 'firefox'});
41
42 // Specify app arguments
43 await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
44})();
45```
46
47
48## API
49
50It uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
51
52### open(target, [options])
53
54Returns 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.
55
56#### target
57
58Type: `string`
59
60The thing you want to open. Can be a URL, file, or executable.
61
62Opens in the default app for the file type. For example, URLs opens in your default browser.
63
64#### options
65
66Type: `Object`
67
68##### wait
69
70Type: `boolean`<br>
71Default: `false`
72
73Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
74
75Note that it waits for the app to exit, not just for the window to close.
76
77On Windows, you have to explicitly specify an app for it to be able to wait.
78
79##### app
80
81Type: `string | string[]`
82
83Specify the app to open the `target` with, or an array with the app and app arguments.
84
85The 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.
86
87You 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.
88
89
90## Related
91
92- [opn-cli](https://github.com/sindresorhus/opn-cli) - CLI for this module
93- [open-editor](https://github.com/sindresorhus/open-editor) - Open files in your editor at a specific line and column
94
95
96## License
97
98MIT © [Sindre Sorhus](https://sindresorhus.com)