What is open?
The 'open' npm package is a simple utility to open a file, URL, or executable in the default program associated with that file type on the user's operating system. It can be used to open resources in the default browser, editor, or any other program.
What are open's main functionalities?
Open URLs in the default web browser
This feature allows you to open a URL in the user's default web browser.
const open = require('open');
open('https://www.example.com');
Open files in the default application
This feature allows you to open a file in the default application associated with its file type, such as a PDF in a PDF viewer.
const open = require('open');
open('path/to/file.pdf');
Open files with a specific application
This feature allows you to open a file with a specific application, bypassing the default application.
const open = require('open');
open('path/to/file.txt', {app: {name: 'notepad'}});
Open files with application and arguments
This feature allows you to open a file with a specific application and pass command-line arguments to the application.
const open = require('open');
open('path/to/file', {app: {name: 'app-name', arguments: ['--arg1', '--arg2']}});
Other packages similar to open
opn
The 'opn' package was the predecessor to 'open' and has since been deprecated in favor of 'open'. It offered similar functionality to open resources with the default application or a specified one.
execa
While 'execa' is more of a process execution tool than a direct alternative to 'open', it can be used to achieve similar results by running system commands to open files or URLs with specific applications.
start
The 'start' package is another alternative that can open files or URLs using the default application. It is less feature-rich compared to 'open' and is specific to Windows.
open.js
open a file or url in the default associated application
install
npm install open
usage
var open = require('open');
open('http://www.google.com');
how it works
- on
win32
uses start
- on
darwin
uses open
- otherwise uses the xdg-open script from Portland
warning
The same care should be taken when calling open as if you were calling
child_process.exec directly. If its an executable it will run in a new shell.