What is is-interactive?
The is-interactive npm package is used to check if a Node.js process is running in an interactive environment. This can be particularly useful for CLI applications that need to adapt their behavior based on whether they're running in a background process or directly in a terminal that can accept user input.
What are is-interactive's main functionalities?
Check if the environment is interactive
This feature allows developers to check if the Node.js process is running in an interactive terminal. The function returns a boolean value: `true` if the process is interactive, and `false` otherwise. This can be useful for CLI tools that need to decide whether to prompt the user for input.
const isInteractive = require('is-interactive');
console.log(isInteractive());
Other packages similar to is-interactive
supports-color
While not identical in functionality, supports-color is a package that can be used alongside is-interactive to further enhance CLI applications. It checks if the terminal supports color, which, combined with interactivity checks, can help tailor output to suit the capabilities of the user's terminal. Unlike is-interactive, which focuses solely on detecting interactivity, supports-color provides insights into the terminal's color capabilities.
inquirer
Inquirer.js is a comprehensive library for creating interactive CLI prompts. Although it serves a different primary purpose compared to is-interactive, it's relevant in the context of building interactive CLI applications. Inquirer assumes an interactive environment to present and handle user prompts, whereas is-interactive could be used to determine whether it's appropriate to use Inquirer or a more straightforward, non-interactive input method.
is-interactive
Check if stdout or stderr is interactive
It checks that the stream is TTY, not a dumb terminal, and not running in a CI.
This can be useful to decide whether to present interactive UI or animations in the terminal.
Install
$ npm install is-interactive
Usage
import isInteractive from 'is-interactive';
isInteractive();
API
isInteractive(options?)
options
Type: object
stream
Type: stream.Writable
Default: process.stdout
The stream to check.
FAQ
Why are you not using ci-info
for the CI check?
It's silly to have to detect individual CIs. They should identify themselves with the CI
environment variable, and most do just that. A manually maintained list of detections will easily get out of date. And if a package using ci-info
doesn't update to the latest version all the time, they will not support certain CIs. It also creates unpredictability as you might assume a CI is not supported and then suddenly it gets supported and you didn't account for that. In addition, some of the manual detections are loose and might cause false-positives which could create hard-to-debug bugs.
Why does this even exist? It's just a few lines.
It's not about the number of lines, but rather discoverability and documentation. A lot of people wouldn't even know they need this. Feel free to copy-paste the code if you don't want the dependency. You might also want to read this blog post.
Related