Open URLs via Operating System
The `openurl` package provides a simple Node.js module to programmatically open URLs, `mailto:` links, or local files using the operating system's default applications. For instance, `http` links will open in the default web browser, and `mailto` links in the default email client. The package's current and final stable version is `1.1.1`, which was last published over 10 years ago in January 2016. Consequently, `openurl` is no longer actively maintained and does not receive updates, bug fixes, or new features. While it may still function for basic use cases in CommonJS environments, it lacks modern features like native ECMAScript Modules (ESM) support and may have unaddressed platform-specific issues. Developers are strongly encouraged to use actively maintained alternatives like the `open` package (by Sindre Sorhus) for better cross-platform compatibility, ESM support, and ongoing reliability.
Common errors
-
TypeError: openurl.open is not a function (or similar for mailto)
cause Attempting to use `openurl` with ESM `import` syntax or incorrect CommonJS usage.fixEnsure you are using `require()` for CommonJS: `const openurl = require('openurl'); openurl.open('...');` If in an ESM project, consider using dynamic `import()` or, preferably, migrate to an ESM-native package like `open`. -
Error: Command failed: xdg-open: not found
cause On Linux systems, `openurl` (or the underlying system command) relies on `xdg-open` which might not be installed or configured correctly, especially in minimal environments or WSL without proper desktop integration.fixInstall `xdg-utils` which provides `xdg-open` on most Linux distributions (e.g., `sudo apt-get install xdg-utils` on Debian/Ubuntu, `sudo yum install xdg-utils` on Fedora/CentOS). Ensure your environment is configured to use graphical applications. -
URL with '=' does not open in Windows, opens explorer folder instead.
cause A known, unresolved bug specific to `openurl` on Windows when the URL string contains an equals sign (`=`), causing the system to misinterpret it as a file path or command.fixThere is no direct fix for this issue within `openurl` due to its abandonment. The recommended solution is to switch to a modern, actively maintained library like `open` (https://www.npmjs.com/package/open), which has better cross-platform handling for such edge cases.
Warnings
- breaking The `openurl` package is abandoned, with its last update over 10 years ago (v1.1.1, January 2016). It does not receive security updates, bug fixes, or compatibility improvements for newer operating systems or Node.js versions. Relying on this package in production is highly discouraged.
- gotcha This package is exclusively a CommonJS module and does not natively support ECMAScript Modules (ESM) `import` syntax. Attempting to `import openurl from 'openurl'` will result in an error in pure ESM environments.
- gotcha The package may exhibit inconsistent behavior or outright failures on certain operating systems or with specific URL formats. For instance, GitHub issues mention problems opening URLs containing `=` on Windows and unexpected side effects like wiping Firefox settings on some systems.
Install
-
npm install openurl -
yarn add openurl -
pnpm add openurl
Imports
- open
import { open } from 'openurl';const openurl = require('openurl'); openurl.open('https://example.com'); - mailto
import { mailto } from 'openurl';const openurl = require('openurl'); openurl.mailto(['recipient@example.com'], { subject: 'Hello', body: 'This is a test' });
Quickstart
const openurl = require('openurl');
// Open a URL in the default web browser
openurl.open('https://www.google.com').then(() => {
console.log('Opened Google in browser.');
}).catch(err => {
console.error('Failed to open URL:', err.message);
});
// Open a mailto link in the default email client with recipients, subject, and body
openurl.mailto(['john.doe@example.com', 'jane.smith@example.com'], {
subject: 'Important Inquiry',
body: 'Dear recipients,\n\nThis is an automatically generated email.\n\nRegards,\nYour App'
}).then(() => {
console.log('Opened mail client for email.');
}).catch(err => {
console.error('Failed to open mailto link:', err.message);
});
// Example of opening a local file (path should be absolute for best results)
// Note: This often opens a directory viewer on some OS if it's a directory path.
// On macOS, it might open the directory in Finder. On Windows, in Explorer.
const filePath = process.platform === 'win32' ? 'C:\\Windows' : '/tmp';
openurl.open(filePath).then(() => {
console.log(`Opened file/directory: ${filePath}`);
}).catch(err => {
console.error('Failed to open file/directory:', err.message);
});