- Advertise with us
- Explore by categories
- Free Online Developer Tools
- Comment Policy
How to use PhantomJS with Node.js
- February 09, 2017
- 47.3K 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.8K 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.3K views
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
- October 17, 2016
- 11.3K views
- October 15, 2017
- 48.4K 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 .
bridge to PhantomJS from Node.js
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.
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.
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 :
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.
PhantomJS - Scriptable Headless Browser
Important : PhantomJS development is suspended until further notice ( more details ).
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 .
- Package Manager
- Script & Interactive
- Release Notes
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.
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.
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.
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).
npm i [email protected]
8 years ago