• Advertise with us
  • Explore by categories
  • Free Online Developer Tools
  • Privacy Policy
  • Comment Policy

How to use PhantomJS with Node.js

Carlos Delgado

Carlos Delgado

  • February 09, 2017
  • 47.3K views

Learn how to use PhantomJS by your own or using a module.

How to use PhantomJS with Node.js

PhantomJS is a headless WebKit scriptable with a JavaScript API multiplatform, available on major operating systems as: Windows, Mac OS X, Linux, and other Unices. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. PhantomJS is fully rendering pages under the hood, so the results can be exported as images. This is very easy to set up, and so is a useful approach for most projects requiring the generation of many browser screenshots (if you're looking how to create only screenshots we recommend you to read instead this article ).

In this article, you will learn how to use PhantomJS with Node.js easily using a module or manipulating it by yourself with Javascript.

Requirements

You will need PhantomJS (installed or a standalone distribution) accesible from the PATH (learn how to add a variable to the PATH in windows here). In case it isn't available in the path, you can specify the executable to PhantomJS in the configuration later.

You can obtain PhantomJS from the following list in every platform (Windows, Linux, MacOS etc)  in the download area of the official website here .

there's no installation process in most of the platforms as you'll get .zip file with two folder, examples and bin (which contains the executable of PhantomJS).

Once you know that PhantomJS is available in your machine, let's get started !

A. Using a module

If you want to use a module to work with PhantomJS in Node.js, you can use the phantom module written by @amir20 . This module offers integration for PhantomJS in Node.js. Although the workflow with Javascript ain't the same that the Javascript that you use to instruct PhantomJS, it's still easy to understand.

To install the module in your project, execute the following command in the terminal:

Once the installation of the module finishes, you will be able to access the module using require("phantom") .

The workflow (of creating the page and then with the page do other things) remains similar to the scripting with plain Javascript in PhantomJS. The page object that is returned with createPage method is a proxy that sends all methods to phantom. Most method calls should be identical to PhantomJS API. You must remember that each method returns a Promise.

The following script will open the Stack Overflow website and will print the html of the homepage in the console:

If you're using Node.js v7+, then you can use the async and await features that this version offers. 

It simplifies the code significantly and is much easier to understand than with Promises.

B. Own implementation

As you probably (should) know, you work with PhantomJS through a js file with some instructions, then the script is executed providing the path of the script as first argument in the command line ( phantomjs /path.to/script-to-execute.js ). To learn how you can interact with PhantomJS using Node.js create the following test script ( phantom-script.js ) that works with PhantomJS perfectly. If you want to test it, use the command phantomjs phantom-script.js in a terminal:

The previous code should simply create a POST request to a website (check obviously that you have internet access while testing it).

Now we are going to use Node.js to cast a child process, this Node script should execute the following command (the same used in the command line):

To do it, we are going to require the child_process module (available by default in Node.js) and save the spawn property in a variable. The child_process.spawn() method spawns a new process using the given command (as first argument), with command line arguments in args (as second argument). If omitted, args defaults to an empty array.

Declare a variable child that has as value the returned value from the used spawn method. In this case the first argument for spawn should be the path to the executable of phantomjs (only phantomjs if it's in the path) and the second parameter should be an array with a single element, the path of the script that phantom should use. From the child variable add a data listener for the stdout (standard output) and stderr (Standard error output). The callback of those listeners will receive an Uint8Array, that you obviously can't read unless you convert it to string. To convert the Uint8Array to its string representation, we are going to use the Uint8ArrToString method (included in the script below). It's a very simple way to do it, if you require scability in your project, we recommend you to read more ways about how to convert this kind of array to a string here .

Create a new script ( executing-phantom.js ) with the following code inside:

As final step execute the previous node script using:

And in the console you should get the following output:

We, personally prefer the self implemented method to work with PhantomJS as the learning curve of the module is steep (at least for those that knows how to work with PhantomJS directly with scripts), besides the documentation ain't so good.

Happy coding !

Senior Software Engineer at EPAM Anywhere . Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Become a more social person

Related articles.

Getting started with Optical Character Recognition (OCR) with Tesseract in Node.js

Getting started with Optical Character Recognition (OCR) with Tesseract in Node.js

  • January 02, 2017
  • 32.8K views

How to use Event Emitters with ES5 and ES6 in Node.js easily

How to use Event Emitters with ES5 and ES6 in Node.js easily

  • May 02, 2017
  • 38.3K views

How to create a sftp client with node.js (SSH2) in Electron Framework

How to create a sftp client with node.js (SSH2) in Electron Framework

  • May 01, 2016
  • 70.3K views

How to connect to a MySQL database with Node.js

How to connect to a MySQL database with Node.js

  • September 20, 2016

How to install and use lwip (Light Weight Image Processor) in Windows for Node.js

How to install and use lwip (Light Weight Image Processor) in Windows for Node.js

  • October 17, 2016
  • 11.3K views

How to obfuscate JavaScript code with Node.js

How to obfuscate JavaScript code with Node.js

  • October 15, 2017
  • 48.4K views

Advertising

Free Digital Ocean Credit

All Rights Reserved © 2015 - 2023

Search code, repositories, users, issues, pull requests...

Provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

bridge to PhantomJS from Node.js

alexscheelmeyer/node-phantom

Name already in use.

Use Git or checkout with SVN using the web URL.

Work fast with our official CLI. Learn more about the CLI .

  • Open with GitHub Desktop
  • Download ZIP

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Node-phantom.

This is a bridge between PhantomJs and Node.js .

It is very much similar to the other bridge available, PhantomJS-Node , but is different in a few ways:

  • Way fewer dependencies/layers.
  • API has the idiomatic error indicator as first parameter to callbacks.
  • Uses plain Javascript instead of Coffeescript.

Requirements

You will need to install PhantomJS first. The bridge assumes that the "phantomjs" binary is available in the PATH.

The only other dependency for using it is socket.io .

For running the tests you will need Mocha . The tests require PhantomJS 1.6 or newer to pass.

You can use it pretty much like you would use PhantomJS-Node, for example this is an adaptation of a web scraping example :

phantom.create(callback,options)

options is an optional object with options for how to start PhantomJS. options.parameters is an array of parameters that will be passed to PhantomJS on the commandline. For example

will start phantom as:

You may also pass in a custom path if you need to select a specific instance of PhantomJS or it is not present in PATH environment. This can for example be used together with the PhantomJS package like so:

Working with the API

Once you have the phantom instance you can use it much as you would the real PhantomJS, node-phantom tries to mimic the api.

An exception is that since this is a wrapper that does network communication to control PhantomJS, all methods are asynchronous and with a callback even when the PhantomJS version is synchronous.

Another notable exception is the page.evaluate method (and page.evaluateAsync method) that since PhantomJS 1.6 has a provision for extra arguments to be passed into the evaluated function. In the node-phantom world these arguments are placed after the callback. So the order is evaluatee, callback, optional arguments. In code it looks like :

You can also have a look at the test folder to see some examples of using the API.

Made by Alex Scheel Meyer. Released to the public domain.

Contributors 11

  • JavaScript 100.0%

PhantomJS - Scriptable Headless Browser

Important : PhantomJS development is suspended until further notice ( more details ).

PhantomJS is a headless web browser scriptable with JavaScript. It runs on Windows, macOS, Linux, and FreeBSD.

Using QtWebKit as the back-end, it offers fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

The following simple script for PhantomJS loads Google homepage, waits a bit, and then captures it to an image.

PhantomJS is an optimal solution for:

Ready to play with PhantomJS? Install and follow the Quick Start guide.

Want to learn more? Read the FAQ , explore more examples , and study the complete API documentation .

For the source code, issue tracker, and other development information, visit github.com/ariya/phantomjs .

PhantomJS 2.1.1

  • Package Manager
  • PackageReference
  • Script & Interactive
  • Dependencies
  • Release Notes

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

Learn more about Target Frameworks and .NET Standard .

This package has no dependencies.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on PhantomJS:

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on PhantomJS:

New features  - Upgraded Qt to 5.5.1 (issue #13377)  - Added support for SSL Client Authentication (issue #11275)  - Added support for context menu event (issue #11429)  - Allow remote debugging to use random port assigned by the OS (issue #13432) Improvements  - Allow outer context to access arbitrary URLs (issue #11217)  - Fixed --local-storage-path and localStoragePath config option (issue #11596)  - Restored --local-url-access=no regression (issue #13412)  - Fixed an issue with loading JS modules contains a last-line comment (issue #12868)  - Fixed an issue with returning binary content in WebServer module (issue #13026)  - Fixed encoded URL loading on Windows (issue #12953)  - Fixed building with GCC 5 (issue #13518)  - Fixed file upload (issue #12506)  - Fixed latest OS detection (issue #13829)

  • 8 Dependencies
  • 519 Dependents
  • 81 Versions

An NPM installer for PhantomJS , headless webkit with JS API.

Building and Installing

Or grab the source and

What this installer is really doing is just grabbing a particular "blessed" (by this module) version of Phantom. As new versions of Phantom are released and vetted, this module will be updated accordingly.

And npm will install a link to the binary in node_modules/.bin as it is wont to do.

Running via node

The package exports a path string that contains the path to the phantomjs binary/executable.

Below is an example of using this package via node.

The major and minor number tracks the version of PhantomJS that will be installed. The patch number is incremented when there is either an installer update or a patch build of the phantom binary.

Pre-2.0, this package was published to NPM as phantomjs . We changed the name to phantomjs at the request of PhantomJS team.

Deciding Where To Get PhantomJS

By default, this package will download phantomjs from our releases . This should work fine for most people.

Downloading from a custom URL

If github is down, or the Great Firewall is blocking github, you may need to use a different download mirror. To set a mirror, set npm config property phantomjs_cdnurl .

Alternatives include https://bitbucket.org/ariya/phantomjs/downloads (the official download site) and http://cnpmjs.org/downloads .

Or add property into your .npmrc file (https://www.npmjs.org/doc/files/npmrc.html)

Another option is to use PATH variable PHANTOMJS_CDNURL .

Using PhantomJS from disk

If you plan to install phantomjs many times on a single machine, you can install the phantomjs binary on PATH. The installer will automatically detect and use that for non-global installs.

Cross-Platform Repositories

PhantomJS needs to be compiled separately for each platform. This installer finds a prebuilt binary for your operating system, and downloads it.

If you check your dependencies into git, and work on a cross-platform team, then you need to tell NPM to rebuild any platform-specific dependencies. Run

as part of your build process. This problem is not specific to PhantomJS, and this solution will work for any NodeJS package with native or platform-specific code.

If you know in advance that you want to install PhantomJS for a specific architecture, you can set the environment variables: PHANTOMJS_PLATFORM (to set target platform) and PHANTOMJS_ARCH (to set target arch), where platform and arch are valid values for process.platform and process.arch .

A Note on PhantomJS

PhantomJS is not a library for NodeJS. It's a separate environment and code written for node is unlikely to be compatible. In particular PhantomJS does not expose a Common JS package loader.

This is an NPM wrapper and can be used to conveniently make Phantom available It is not a Node JS wrapper.

I have had reasonable experiences writing standalone Phantom scripts which I then drive from within a node program by spawning phantom in a child process.

Read the PhantomJS FAQ for more details: http://phantomjs.org/faq.html

An extra note on Linux usage, from the PhantomJS download page:

There is no requirement to install Qt, WebKit, or any other libraries. It however still relies on Fontconfig (the package fontconfig or libfontconfig, depending on the distribution).

Troubleshooting

Installation fails with spawn enoent.

This is NPM's way of telling you that it was not able to start a process. It usually means:

  • node is not on your PATH, or otherwise not correctly installed.
  • tar is not on your PATH. This package expects tar on your PATH on Linux-based platforms.

Check your specific error message for more information.

Installation fails with Error: EPERM or operation not permitted or permission denied

This error means that NPM was not able to install phantomjs to the file system. There are three major reasons why this could happen:

  • You don't have write access to the installation directory.
  • The permissions in the NPM cache got messed up, and you need to run npm cache clean to fix them.
  • You have over-zealous anti-virus software installed, and it's blocking file system writes.

Installation fails with Error: read ECONNRESET or Error: connect ETIMEDOUT

This error means that something went wrong with your internet connection, and the installer was not able to download the PhantomJS binary for your platform. Please try again.

I tried again, but I get ECONNRESET or ETIMEDOUT consistently.

Do you live in China, or a country with an authoritarian government? We've seen problems where the GFW or local ISP blocks github, preventing the installer from downloading the binary.

Try visiting the download page manually. If that page is blocked, you can try using a different CDN with the PHANTOMJS_CDNURL env variable described above.

I am behind a corporate proxy that uses self-signed SSL certificates to intercept encrypted traffic.

You can tell NPM and the PhantomJS installer to skip validation of ssl keys with NPM's strict-ssl setting:

WARNING: Turning off strict-ssl leaves you vulnerable to attackers reading your encrypted traffic, so run this at your own risk!

I tried everything, but my network is b0rked. What do I do?

If you install PhantomJS manually, and put it on PATH, the installer will try to use the manually-installed binaries.

I'm on Debian or Ubuntu, and the installer failed because it couldn't find node

Some Linux distros tried to rename node to nodejs due to a package conflict. This is a non-portable change, and we do not try to support this. The official documentation recommends that you run apt-get install nodejs-legacy to symlink node to nodejs on those platforms, or many NodeJS programs won't work properly.

Contributing

Questions, comments, bug reports, and pull requests are all welcome. Submit them at the project on GitHub . If you haven't contributed to an Medium project before please head over to the Open Source Project and fill out an OCLA (it should be pretty painless).

Bug reports that include steps-to-reproduce (including code) are the best. Even better, make them in the form of pull requests.

Dan Pupius ( personal website ) and Nick Santos , supported by A Medium Corporation .

Copyright 2012 A Medium Corporation .

Licensed under the Apache License, Version 2.0. See the top-level file LICENSE.txt and (http://www.apache.org/licenses/LICENSE-2.0).

Package Sidebar

npm i [email protected]

Git github.com/Medium/phantomjs

github.com/Medium/phantomjs

Last publish

8 years ago

Collaborators

mediumeng

IMAGES

  1. NodeJS : Deploy phantomJS to node.js app?

    node phantomjs

  2. How to create a screenshot from a website or html with PhantomJS in Node.js

    node phantomjs

  3. NodeJS : Implementing waitFor functionality with phantomjs-node

    node phantomjs

  4. [Solved] Installing phantomjs with node in docker

    node phantomjs

  5. How to put phantomjs on $PATH

    node phantomjs

  6. How to Install PhantomJS

    node phantomjs

VIDEO

  1. Exporting Pentaho CDE Dashboards as PDF (with PhantomJS)

  2. nodejs

  3. NodeJS 04

  4. Create Rest API with NodeJS, TypeScript, Express, MongoDB step by step 2022

  5. NodeJS

  6. NodeJS : using mocha-phantomjs to automate functional testing

COMMENTS

  1. phantomjs

    PhantomJS is not a library for NodeJS. It's a separate environment and code written for node is unlikely to be compatible. In particular PhantomJS does not expose a Common JS package loader. This is an NPM wrapper and can be used to conveniently make Phantom available It is not a Node JS wrapper.

  2. How to use PhantomJS with Node.js

    In this article, you will learn how to use PhantomJS with Node.js easily using a module or manipulating it by yourself with Javascript. Requirements You will need PhantomJS (installed or a standalone distribution) accesible from the PATH (learn how to add a variable to the PATH in windows here).

  3. javascript

    phantomjs-node isn't an official supported npm package for phantomjs. Instead, it implements a "nauseously clever bridge" between node and phantom by creating a web server that uses websockets to serve as an IPC channel between node and phantom.

  4. GitHub

    PhantomJS is not a library for NodeJS. It's a separate environment and code written for node is unlikely to be compatible. In particular PhantomJS does not expose a Common JS package loader. This is an NPM wrapper and can be used to conveniently make Phantom available. It is not a Node JS wrapper.

  5. amir20/phantomjs-node: PhantomJS integration module for NodeJS

    You can quickly test any website with phantomjs-node without needing to install the package. $ npx phantom@latest https://stackoverflow.com/. The above command is very useful to test if your website works on older browsers. I frequently use it to ensure polyfills have been installed correctly.

  6. PhantomJS

    answer. Q: Why do I get build failure with PhantomJS 1.5 after successfully compiled 1.4? A: PhantomJS has a completely different build workflow in version 1.5 compared to its previous versions (read the details). If the working directory still has some left-over from 1.4 build process, there will be a compile error, such as:

  7. phantomjs2

    PhantomJS is not a library for NodeJS. It's a separate environment and code written for node is unlikely to be compatible. In particular PhantomJS does not expose a Common JS package loader. This is an NPM wrapper and can be used to conveniently make Phantom available It is not a Node JS wrapper.

  8. phantomjs-prebuilt

    Running via node The package exports a path string that contains the path to the phantomjs binary/executable. Below is an example of using this package via node. var path = require('path') var childProcess = require('child_process') var phantomjs = require('phantomjs-prebuilt') var binPath = phantomjs.path var childArgs = [

  9. Download PhantomJS

    New to PhantomJS? Read and study the Quick Startguide. Windows Download phantomjs-2.1.1-windows.zip(17.4 MB) and extract (unzip) the content. The executable phantomjs.exeis ready to use. Note: For this static build, the binary is self-contained with no external dependency. It will run on a fresh install of Windows Vista or later versions.

  10. phantom

    PhantomJS integration module for NodeJS. Latest version: 6.3.0, last published: 4 years ago. Start using phantom in your project by running `npm i phantom`. There are 364 other projects in the npm registry using phantom.

  11. Related Projects

    phantomjs-node provides PhantomJS bridge for Node.js applications. node-phantom is a bridge between PhantomJS and Node.js. phantom-proxy provides a proxy interface for PhantomJS which runs inside of node.

  12. alexscheelmeyer/node-phantom: bridge to PhantomJS from Node.js

    You will need to install PhantomJS first. The bridge assumes that the "phantomjs" binary is available in the PATH. The only other dependency for using it is socket.io. For running the tests you will need Mocha. The tests require PhantomJS 1.6 or newer to pass. Installing npm install node-phantom Usage

  13. node-phantom-simple

    Simple and reliable bridge between Node.js and PhantomJS / SlimerJS. Latest version: 2.2.4, last published: 8 years ago. Start using node-phantom-simple in your project by running `npm i node-phantom-simple`. There are 32 other projects in the npm registry using node-phantom-simple.

  14. PhantomJS

    PhantomJS is a headless web browser scriptable with JavaScript. It runs on Windows, macOS, Linux, and FreeBSD. Using QtWebKit as the back-end, it offers fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

  15. node-phantom

    bridge between node.js and PhantomJS. Latest version: 0.2.5, last published: 10 years ago. Start using node-phantom in your project by running `npm i node-phantom`. There are 37 other projects in the npm registry using node-phantom.

  16. Error installing PhantomJS with NPM / NodeJS

    I recently cannot install PhantomJS anymore inside of my Vagrant Box. "npm install phantomjs" fails. When i check the extracted folder after the installation fails in "/tmp/phantomjs/phantomjs-1.9.8-linux-x86_64.tar.bz2-extract-...", the folder is empty. The results is: [exec] Copying extracted folder /tmp/phantomjs/phantomjs-1.9.8-linux-x86_64 ...

  17. NuGet Gallery

    NReco.PhantomJS is a .NET wrapper for running PhantomJS (headless WebKit browser) from C#/.NET code. PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

  18. node-qunit-phantomjs

    Report malware. Run QUnit unit tests in a headless PhantomJS instance without using Grunt.. Latest version: 2.1.1, last published: 4 years ago. Start using node-qunit-phantomjs in your project by running `npm i node-qunit-phantomjs`. There are 5 other projects in the npm registry using node-qunit-phantomjs.

  19. phantomjs

    PhantomJS is not a library for NodeJS. It's a separate environment and code written for node is unlikely to be compatible. In particular PhantomJS does not expose a Common JS package loader. This is an NPM wrapper and can be used to conveniently make Phantom available It is not a Node JS wrapper.