NPM installer and wrapper for PhantomJS headless WebKit browser with JavaScript API
npx @tessl/cli install tessl/npm-phantomjs@1.9.0PhantomJS is an NPM installer and wrapper for the PhantomJS headless WebKit browser with JavaScript API. This package automatically downloads and installs a platform-specific PhantomJS binary and provides Node.js utilities to locate and execute PhantomJS programmatically. It enables headless browser automation, web scraping, testing, and server-side browser capabilities without requiring a GUI.
npm install phantomjsconst phantomjs = require('phantomjs');For ES modules (if supported in your environment):
import phantomjs from 'phantomjs';const phantomjs = require('phantomjs');
const childProcess = require('child_process');
const path = require('path');
// Get the path to the PhantomJS binary
const binPath = phantomjs.path;
// Execute a PhantomJS script
const scriptPath = path.join(__dirname, 'my-script.js');
const childArgs = [scriptPath, 'arg1', 'arg2'];
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
if (err) {
console.error('PhantomJS execution failed:', err);
return;
}
console.log('PhantomJS output:', stdout);
});Provides the absolute path to the installed PhantomJS binary executable.
/**
* Absolute path to the PhantomJS binary executable.
* Returns null if the binary is not yet installed (e.g., during installation).
*/
phantomjs.path; // {string|null}Provides information about the target platform and architecture.
/**
* Target platform identifier (e.g., 'linux', 'darwin', 'win32').
* May be undefined if location module fails to load during installation.
*/
phantomjs.platform; // {string|undefined}
/**
* Target architecture identifier (e.g., 'x64', 'ia32').
* May be undefined if location module fails to load during installation.
*/
phantomjs.arch; // {string|undefined}Provides the version of the PhantomJS binary installed by this package.
/**
* Version string of the PhantomJS binary installed by this package.
* Note: This is the PhantomJS binary version (e.g., '1.9.8'),
* which may differ from the npm package version.
*/
phantomjs.version; // {string}Utility function to clean PATH environment variable by removing node_modules and ./bin entries.
/**
* Removes node_modules and ./bin entries from a PATH string to avoid
* conflicts with npm-installed binaries.
* @param {string} path - PATH environment variable string
* @returns {string} Cleaned PATH string
*/
phantomjs.cleanPath(path);Usage Example:
const phantomjs = require('phantomjs');
// Clean the current PATH
const originalPath = process.env.PATH;
const cleanedPath = phantomjs.cleanPath(originalPath);
console.log('Original PATH:', originalPath);
console.log('Cleaned PATH:', cleanedPath);
// Use cleaned PATH to avoid finding npm-installed binaries
process.env.PATH = cleanedPath;The package also provides a command-line executable that forwards all arguments to the PhantomJS binary.
# Direct execution via npm bin linking
phantomjs --version
phantomjs script.js arg1 arg2
# Or via npx
npx phantomjs --versionThe phantomjs package consists of several key components:
npm installbin/phantomjs) that spawns the actual PhantomJS binary with stdio forwardingphantomjs.path returns null if the binary is not installed (typically during installation)phantomjs.platform and phantomjs.arch may be undefined if the location module fails to load during installationchildProcess.execFile()The package supports several environment variables and npm configuration options:
Example:
# Install with custom CDN
npm install phantomjs --phantomjs_cdnurl=https://bitbucket.org/ariya/phantomjs/downloads
# Or set environment variable
PHANTOMJS_CDNURL=https://bitbucket.org/ariya/phantomjs/downloads npm install phantomjs