readme.md (6103B)
1 # open 2 3 > Open stuff like URLs, files, executables. Cross-platform. 4 5 This is meant to be used in command-line tools and scripts, not in the browser. 6 7 If you need this for Electron, use [`shell.openPath()`](https://www.electronjs.org/docs/api/shell#shellopenpathpath) instead. 8 9 This package does not make any security guarantees. If you pass in untrusted input, it's up to you to properly sanitize it. 10 11 #### Why? 12 13 - Actively maintained. 14 - Supports app arguments. 15 - Safer as it uses `spawn` instead of `exec`. 16 - Fixes most of the original `node-open` issues. 17 - Includes the latest [`xdg-open` script](https://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux. 18 - Supports WSL paths to Windows apps. 19 20 ## Install 21 22 ``` 23 $ npm install open 24 ``` 25 26 ## Usage 27 28 ```js 29 const open = require('open'); 30 31 // Opens the image in the default image viewer and waits for the opened app to quit. 32 await open('unicorn.png', {wait: true}); 33 console.log('The image viewer app quit'); 34 35 // Opens the URL in the default browser. 36 await open('https://sindresorhus.com'); 37 38 // Opens the URL in a specified browser. 39 await open('https://sindresorhus.com', {app: {name: 'firefox'}}); 40 41 // Specify app arguments. 42 await open('https://sindresorhus.com', {app: {name: 'google chrome', arguments: ['--incognito']}}); 43 44 // Open an app 45 await open.openApp('xcode'); 46 47 // Open an app with arguments 48 await open.openApp(open.apps.chrome, {arguments: ['--incognito']}); 49 ``` 50 51 ## API 52 53 It uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms. 54 55 ### open(target, options?) 56 57 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. 58 59 #### target 60 61 Type: `string` 62 63 The thing you want to open. Can be a URL, file, or executable. 64 65 Opens in the default app for the file type. For example, URLs opens in your default browser. 66 67 #### options 68 69 Type: `object` 70 71 ##### wait 72 73 Type: `boolean`\ 74 Default: `false` 75 76 Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app. 77 78 Note that it waits for the app to exit, not just for the window to close. 79 80 On Windows, you have to explicitly specify an app for it to be able to wait. 81 82 ##### background <sup>(macOS only)</sup> 83 84 Type: `boolean`\ 85 Default: `false` 86 87 Do not bring the app to the foreground. 88 89 ##### newInstance <sup>(macOS only)</sup> 90 91 Type: `boolean`\ 92 Default: `false` 93 94 Open a new instance of the app even it's already running. 95 96 A new instance is always opened on other platforms. 97 98 ##### app 99 100 Type: `{name: string | string[], arguments?: string[]} | Array<{name: string | string[], arguments: string[]}>` 101 102 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. 103 104 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. 105 106 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. 107 108 The app `arguments` are app dependent. Check the app's documentation for what arguments it accepts. 109 110 ##### allowNonzeroExitCode 111 112 Type: `boolean`\ 113 Default: `false` 114 115 Allow the opened app to exit with nonzero exit code when the `wait` option is `true`. 116 117 We do not recommend setting this option. The convention for success is exit code zero. 118 119 ### open.apps 120 121 An object containing auto-detected binary names for common apps. Useful to work around [cross-platform differences](#app). 122 123 ```js 124 const open = require('open'); 125 126 await open('https://google.com', { 127 app: { 128 name: open.apps.chrome 129 } 130 }); 131 ``` 132 133 #### Supported apps 134 135 - [`chrome`](https://www.google.com/chrome) - Web browser 136 - [`firefox`](https://www.mozilla.org/firefox) - Web browser 137 - [`edge`](https://www.microsoft.com/edge) - Web browser 138 139 ### open.openApp(name, options?) 140 141 Open an app. 142 143 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. 144 145 #### name 146 147 Type: `string` 148 149 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. 150 151 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. 152 153 #### options 154 155 Type: `object` 156 157 Same options as [`open`](#options) except `app` and with the following additions: 158 159 ##### arguments 160 161 Type: `string[]`\ 162 Default: `[]` 163 164 Arguments passed to the app. 165 166 These arguments are app dependent. Check the app's documentation for what arguments it accepts. 167 168 ## Related 169 170 - [open-cli](https://github.com/sindresorhus/open-cli) - CLI for this module 171 - [open-editor](https://github.com/sindresorhus/open-editor) - Open files in your editor at a specific line and column 172 173 --- 174 175 <div align="center"> 176 <b> 177 <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> 178 </b> 179 <br> 180 <sub> 181 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. 182 </sub> 183 </div>