- Advertise with us
- Explore by categories
- Free Online Developer Tools
- Comment Policy
How to use PhantomJS with Node.js
- February 09, 2017
- 47.4K views
Learn how to use PhantomJS by your own or using a module.
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
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 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
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
- May 02, 2017
- 38.3K views
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
- September 20, 2016
- 84.1K views
How to install and use lwip (Light Weight Image Processor) in Windows for Node.js
- October 17, 2016
- 11.3K views
- October 15, 2017
- 48.5K views
All Rights Reserved © 2015 - 2023
Search code, repositories, users, issues, pull requests...
We read every piece of feedback, and take your input very seriously.
Use saved searches to filter your results more quickly.
To see all available qualifiers, see our documentation .
NPM wrapper for installing 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.
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.
- 391 commits
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.
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.
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.
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.
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).
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).
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 ).
Used by 105k.
- Exchange rates
- Change City
AVANGARD in Elektrostal, Russia
- Show the bank offices on map
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.
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.
Download phantomjs-2.1.1-linux-i686.tar.bz2 (23.0 MB) and extract the content.
Binary packages are available via pkg:
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.
Sha-256 checksums, acknowledgement.
Download service is kindly provided by BitBucket and previously by Google Code Project Hosting.