• 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.4K 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.9K 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.5K views

How to connect to a MySQL database with Node.js

How to connect to a MySQL database with Node.js

  • September 20, 2016
  • 84.1K views

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.5K 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

NPM wrapper for installing phantomjs

Medium/phantomjs

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

  • 391 commits

phantomjs-prebuilt

WARNING: This repository and NPM package are now deprecated since PhantomJS development had been suspended.

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

Build Status

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.

Or exec() method is also provided for convenience:

Note: childProcess.spawn() is called inside exec() .

Running with WebDriver

run() method detects when PhantomJS gets ready. It's handy to use with WebDriver (Selenium).

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-prebuilt at the request of PhantomJS team.

Continuous Integration

Please do not download PhantomJS for every CI job because it can quickly overload our CDNs. Instead take advantage of CI caching.

In Travis-CI add the following to your .travis.yml to enable caching & avoid repeated downloads of PhantomJS.

.travis.yml

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.
  • bzip2 is not on your PATH.

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.

I'm using an unsupported version of Linux or an ARM processor. I get errors about "Unexpected platform or architecture". What do I do?

We only have binaries available for common OS / processor configurations. Sorry.

You may be able to get a PhantomJS binary from your operating system's package manager. Or you can build your own from source. If you put that binary on PATH, this installer will use it (see "Deciding Where to Get PhantomJS" above).

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 ).

Releases 20

Used by 105k.

@CicakKayang

Contributors 66

  • JavaScript 100.0%
  • Exchange rates
  • Change City

AVANGARD in Elektrostal, Russia

Avangard offices.

Offices of

  • Show the bank offices on map

Download PhantomJS

New to PhantomJS? Read and study the Quick Start guide.

Download phantomjs-2.1.1-windows.zip (17.4 MB) and extract (unzip) the content.

The executable phantomjs.exe is 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. There is no requirement to install Qt, WebKit, or any other libraries.

Download phantomjs-2.1.1-macosx.zip (16.4 MB) and extract (unzip) the content.

Note : For this static build, the binary is self-contained with no external dependency. It will run on a fresh install of OS X 10.7 (Lion) or later versions. There is no requirement to install Qt or any other libraries.

Linux 64-bit

Download phantomjs-2.1.1-linux-x86_64.tar.bz2 (22.3 MB) and extract the content.

Note : For this static build, the binary is self-contained. 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). The system must have GLIBCXX_3.4.9 and GLIBC_2.7.

Linux 32-bit

Download phantomjs-2.1.1-linux-i686.tar.bz2 (23.0 MB) and extract the content.

Binary packages are available via pkg:

Source Code

To get the source code, check the official git repository: github.com/ariya/phantomjs .

To compiled PhantomJS from source (not recommended, unless it is absolutely necessary), follow the build instructions .

To verify the integrity of the downloaded files, use the following checksums.

MD5 Checksums

Sha-256 checksums, acknowledgement.

Download service is kindly provided by BitBucket and previously by Google Code Project Hosting.

IMAGES

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

    phantomjs nodejs

  2. How to Install PhantomJS

    phantomjs nodejs

  3. Introduction To PhantomJS: A Headless Browser For Automation Testing

    phantomjs nodejs

  4. PhantomJS

    phantomjs nodejs

  5. How To Install PhantomJS in a VPS

    phantomjs nodejs

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

    phantomjs nodejs

VIDEO

  1. 03 多线程爬虫实例1 recv 多线程爬虫和Selenium+PhantomJS

  2. FullStack Development with NodeJs & ReactJs

  3. Headless Selenium Testing with Python and PhantomJS

  4. nodejs

  5. KOF MUGEN = Sophie Chrysocolla VS Clio Sapphire

  6. NodeJS 04

COMMENTS

  1. phantomjs

    phantomjs An NPM installer for PhantomJS, headless webkit with JS API. DEPRECATED Pre-2.0, this package was published to NPM as phantomjs . We changed the name to phantomjs-prebuilt at the request of PhantomJS team. Please update your package references from phantomjs to phantomjs-prebuilt Building and Installing npm install phantomjs

  2. How to use PhantomJS with Node.js

    How to use PhantomJS with Node.js Carlos Delgado February 09, 2017 47.3K views Learn how to use PhantomJS by your own or using a module. 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.

  3. phantomjs

    phantomjs An NPM installer for PhantomJS, headless webkit with JS API. Building and Installing npm install phantomjs Or grab the source and node ./install.js What this installer is really doing is just grabbing a particular "blessed" (by this module) version of Phantom.

  4. javascript

    can phantomjs work with node.js? Ask Question Asked 10 years, 6 months ago Modified 6 years, 10 months ago Viewed 39k times 28 I would like to use phantomjs in my node.js script. there is a phantomjs-node library.. but unfortunately the author used this weird coffee script code to explain what he's doing:

  5. 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.

  6. Quick Start with PhantomJS

    Quick Start with PhantomJS. PhantomJS is a command-line tool. Make sure that you are familiar with the use of the command prompt or PowerShell (on Windows) or a terminal (on macOS and Linux).. This instruction assumes that PhantomJS is installed and placed somewhere in the PATH (e.g. see this tutorial for Windows users).. Hello, World!

  7. PhantomJS

    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: phantom.cpp:305: error: no matching function for call to 'QWebFrame::evaluateJavaScript(QString, QString)

  8. 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.

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

    GitHub - amir20/phantomjs-node: PhantomJS integration module for NodeJS amir20 / phantomjs-node Public archive master 25 branches 62 tags Code 761 commits .github Updates template to have env info 7 years ago .vscode Enables prettier with AirBnB style code ( #695) 6 years ago bin Fixes lint 4 years ago decls

  10. 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.

  11. Download PhantomJS

    Download PhantomJS. New to PhantomJS? Read and study the Quick Start guide.. Windows. Download phantomjs-2.1.1-windows.zip (17.4 MB) and extract (unzip) the content.. The executable phantomjs.exe is 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.

  12. Related Projects

    phantomjs-screenshots, a system for running screenshot tasks defined in JSON format (Node.js) web-service manet (Node.js) screenshot-app (Node.js) screenshot-as-a-service (Node.js) screenshot-service a pure phantomJS project to capture screenshot of any page with various configuration options screenshot (Dancer) screenshot-webservice (Play2)

  13. Error installing PhantomJS with NPM / NodeJS

    2 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:

  14. PhantomJS

    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.

  15. Electrostal School #1

    Electrostal School #1 Московская область, Электросталь, улица Корешкова, дом 16 Elektrostal, Moscow Oblast

  16. "AVANGARD" JSB in Elektrostal, addresses, phones and operation time of

    1. Address: Elektrostal, Lenina Avenue, 1V, Avenue» Phones: +7 (495) 234-23-24, +7 (495) 234-98-98, +7 (495) 737-73-73 (add. 08972) Operation time:

  17. S-PRO SISTEMS, OOO Company Profile

    S-PRO SISTEMS, OOO Company Profile | Elektrostal, Moscow region, Russian Federation | Competitors, Financials & Contacts - Dun & Bradstreet

  18. SOVREMENNYE TEKHNOLOGII, OOO Company Profile

    Industry: Other Support Services , Other Specialty Trade Contractors , Soap, Cleaning Compound, and Toilet Preparation Manufacturing , General Freight Trucking , Warehousing and Storage See All Industries, Packaging and labeling services, Excavation work, Soap and other detergents, Soap and other detergents, nec, Trucking, except local General warehousing and storage See Fewer Industries

  19. Download PhantomJS

    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.