2026-10-13 14:38:28 +00:00
2026-10-13 14:38:28 +00:00
…
2026-10-13 14:38:28 +00:00
…
2026-10-13 14:38:28 +00:00
…

@push.rocks/smartopen

open things

Install

To start using @push.rocks/smartopen, you'll first need to install it using npm. In your terminal, navigate to your project directory and run:

npm install @push.rocks/smartopen --save

This command will add @push.rocks/smartopen as a dependency to your project, allowing you to use it in your TypeScript applications.

Usage

Getting Started with SmartOpen

@push.rocks/smartopen provides an easy way to programmatically open URLs using Node.js. This can be particularly useful in desktop applications or server-side scripts where you want to trigger opening a web page in the default browser. Below, you'll find comprehensive examples to get you started.

Before diving into the examples, ensure you have TypeScript setup in your project. TypeScript provides strong types for JavaScript, which enhances development through type checking and better tooling support.

Basic Usage

Here is the simplest way to open a URL:

import { openUrl } from '@push.rocks/smartopen';

async function openWebPage() {
  await openUrl('https://example.com');
}

openWebPage().then(() => {
  console.log('Web page opened successfully');
}).catch((error) => {
  console.error('Failed to open web page:', error);
});

This function imports the openUrl method from @push.rocks/smartopen and executes it with a URL string. It opens the provided URL in the default web browser.

Advanced Usage: Handling in Continuous Integration Environments

@push.rocks/smartopen intelligently detects if it's running in a Continuous Integration (CI) environment and avoids opening the browser in such cases. This is practical for automated testing or deployment scripts where opening a browser window would be unnecessary or problematic.

Here's how you can utilize @push.rocks/smartopen in an environment-aware manner:

import { openUrl } from '@push.rocks/smartopen';

async function openLinkConditionally(url: string) {
  const result = await openUrl(url);
  if(result === null) {
    console.log('Environment detected as CI, skipping browser opening.');
  } else {
    console.log('Browser should open shortly:', url);
  }
}

openLinkConditionally('https://example.com')
  .catch((error) => console.error('An error occurred:', error));

In this example, the openUrl function returns null if it detects that it's being run in a CI environment, allowing the calling code to handle this scenario appropriately.

What openUrl launches

openUrl(url) returns null when process.env.CI is exactly the string true. Otherwise it spawns the platform opener with child_process.spawn, calls unref() on it so it does not keep your process alive, and resolves to that ChildProcess without waiting for it to finish. A non-string url rejects with TypeError: Expected a \target``.

Host Command Arguments Spawn options
macOS open the url none
Windows %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -NoProfile -NonInteractive –ExecutionPolicy Bypass -EncodedCommand <Start 'url'> windowsVerbatimArguments: true
WSL (outside Docker) <drives mount>c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe as on Windows none
Linux, Docker and other systems xdg-open from PATH the url stdio: 'ignore', detached: true
  • The PowerShell command is Start '<url>', encoded as Base64 UTF-16LE. PowerShell expands nothing inside single quotes, and every single-quote character it recognises in the url (', ‘, ’, ‚, ‛) is doubled, so a url cannot end the string or run code.
  • WSL is detected from the kernel release or /proc/version containing microsoft, unless /.dockerenv exists or /proc/self/cgroup mentions docker. The drives mount point is the root from /etc/wsl.conf, else /mnt/.
  • An empty url is not passed to open or xdg-open, and PowerShell receives a bare Start.

These are the same commands, arguments and options that the open package (8.4.2) used before smartopen implemented them itself, with one difference: open ran a copy of the xdg-open script bundled in its package, while smartopen runs the xdg-open installed on the system.

If the opener cannot start, for example on a minimal Linux container without xdg-open, smartopen does not crash your process: it listens for the child's error event itself and prints one warning such as smartopen: could not open https://example.com: xdg-open is not installed (spawn xdg-open ENOENT). The promise still resolves to the ChildProcess, and listeners you add receive the error event too:

const child = await openUrl('https://example.com');
child?.on('error', (error) => console.log(`No browser was opened: ${error.message}`));

Conclusion

@push.rocks/smartopen provides a straightforward and effective solution for opening URLs from Node.js scripts, with smart handling for CI environments. Whether you're building a desktop application, developing a server-side application that interacts with web services, or creating automation scripts that open web pages, @push.rocks/smartopen offers a reliable method to achieve your goals.

Remember, the examples above demonstrate the basic and advanced usage of @push.rocks/smartopen. Depending on your specific needs, you might need to adjust the logic or combine it with other modules to create more complex and powerful applications.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license.md file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

S
Description
A library for opening URLs or files in the default application
Readme
625 KiB
Languages
TypeScript 100%