- Source Code
Before reporting an issue, make sure you are using the latest version:
All examples are designed to work with the latest version of PhantomJS. If some examples do not work, make sure that there is not more than one version of PhantomJS installed in the system. Multiple versions may lead to conflict as to which one is being invoked in the terminal.
If the data is not transferred correctly, check if the network works as expected.
Since every network request and response can be “sniffed”, add simple callbacks to facilitate such troubleshooting. For more details, see the wiki page on Network Monitoring . An example of a simple logging:
Transport Layer Security (TLS) and Secure Sockets Layer (SSL) are necessary to access encrypted data, for example when connecting to a server using HTTPS. Thus, if PhantomJS works well with HTTP but it shows some problem when using HTTPS, the first useful thing to check it whether the SSL libraries, usually OpenSSL, have been installed properly.
On Windows, the default proxy setting may cause a massive network latency (see Known Issues in the release note). The workaround is to disable proxy completely, e.g. by launching PhantomJS with --proxy-type=none command-line argument.
SELinux can stop PhantomJS working. The solution given here, to create a custom policy, has been reported to work: http://serverfault.com/a/430499/87322
To easily catch an error occured in a web page, whether it is a syntax error or other thrown exception, an onError handler for the WebPage object has been added. An example on such a handler is:
Remote debugging permits inspection of the script and web page via another WebKit-based browser (Safari and Chrome). This is achieved by launching PhantomJS with the new option, as in this example
After that, open Safari/Chrome/Chromium and go to the URL http://ipaddress:9000 . If you executed the phantomjs command on the same machine, it will be http://127.0.0.1:9000
You will see a list of links. Click the first (it may say “about:blank”). It will show a version of the Web Inspector interface which in this case works on the script being tested..
To set breakpoints, look for your script in the drop-down in the Scripts tab; it should have the same URL that you just clicked on.
After setting breakpoints (or if you are using debugger instead), to run your script, simply enter the __run() command in the Web Inspector Console. Alternatively, use --remote-debugger-autorun=yes command-line argument to have your script launched immediately.
To debug inside a target web page requires two inspectors and a multi-step process using code like this:
- Instrument your PhantomJS script with two debugger; lines as in the example above.
- Start PhantomJS on the command line with the --remote-debugger-port=9000 option.
- Open http://127.0.0.1:9000/ in a Webkit-based web browser.
- There should be a bulleted list with a single link. Click on it to open a web inspector that operates in the PhantomJS context. (Sometimes the link is invisible because it contains no text; if you see nothing, navigate directly to http://127.0.0.1:9000//webkit/inspector/inspector.html?page=1.)
- Change to the Console section of the inspector and execute the statement __run() . This should cause the script to run until your first debugger; statement.
- Open a new browser tab and return to the debugging portal at http://127.0.0.1:9000. There should now be a second entry in the bulleted list.
- Click on this entry to open a new debugger that operates in the context of your page inside PhantomJS.
- Return to the first web inspector tab and click the “Continue” button in the debugger.
- In the second tab, you should now find the JS debugger paused at the second debugger; statement. You can now debug your page’s JS using the full capabilities of the Webkit inspector.
Interactive Mode (REPL)
If PhantomJS is launched without any argument, it starts in the so-called interactive mode, also known as REPL (read-eval-print-loop). This mode allows a faster cycle of experiment and script prototyping. PhantomJS REPL supports the expected features: command editing, persistent history, and autocomplete (with Tab key). Read more about REPL .
- Get Started
- D Inter Process Communication
- D Command Line Interface
- D Troubleshooting
- D Best Practices
- D Tips and Tricks
- D Supported Web Standards
- D Who's using PhantomJS?
- D Related Projects
- D Contributing
- D Test Suite
- D Release Preparation
- D Crash Reporting
- D Bug Reporting
© Copyright 2010-2020 Ariya Hidayat — Homepage design by Maurice Svay — Documentation design by Jamie Mason .
This package has been deprecated
- 3 Dependencies
- 364 Dependents
- 108 Versions
phantom - Fast NodeJS API for PhantomJS
Super easy to use
Using Node v7.9.0+ you can run the above example with node file.js
See examples folder for more ways to use this module.
Use it with npx
You can quickly test any website with phantomjs-node without needing to install the package.
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.
Deprecation warnings of PhantomJs
In March 2018, the owner of PhantomJS announced suspension of development. There hasn't been any updates since. Since phantomjs-node is only a wrapper around phantomjs, then you should use it at your own risk because the underlying dependency is no longer supported. I plan to maintain this project until usage has dropped significantly.
Node v6.x and later.
Latest version of phantom does require Node v6.x and later . You can install with
To use version 3.x you need to have at least Node v5+. You can install it using
Versions older than 5.x, install with
Creating new phantom instances with phantom.create() can be slow. If you are frequently creating new instances and destroying them, as a result of HTTP requests for example, it might be worth creating a pool of instances that are re-used.
See the phantom-pool module for more info.
To run the test suite, first install the dependencies, then run npm test :
This package is under development. Pull requests are welcomed. Please make sure tests are added for new functionalities and that your build does pass in TravisCI.
The current lead maintainer is Amir Raminfar
List of all contributors
npm i phantom
Downloads Weekly Downloads
Unpacked size, total files, last publish.
4 years ago
- Advertise with us
- Explore by categories
- Free Online Developer Tools
- Comment Policy
How to use PhantomJS with Node.js
- February 09, 2017
- 47.9K 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 Software Medico . 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
- 33.5K views
How to use Event Emitters with ES5 and ES6 in Node.js easily
- May 02, 2017
- 38.8K views
How to create a sftp client with node.js (SSH2) in Electron Framework
- May 01, 2016
- 71.3K views
How to connect to a MySQL database with Node.js
- September 20, 2016
- 84.8K views
How to install and use lwip (Light Weight Image Processor) in Windows for Node.js
- October 17, 2016
- 11.6K views
- October 15, 2017
- 49.1K views
All Rights Reserved © 2015 - 2024
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 .
Both error when install using "npm install phantomjs" and "node ./install.js" on mac #156
zhaoyingx commented May 8, 2014
dpup commented May 8, 2014
Sorry, something went wrong.
ghost commented May 14, 2014
Zhaoyingx commented may 15, 2014.
fengmk2 commented May 21, 2014
kylinsoong commented Jan 25, 2016
Fengmk2 commented jan 25, 2016.
humitos commented Mar 7, 2017
Adrian0350 commented Apr 12, 2018
beebird commented Dec 13, 2018
Successfully merging a pull request may close this issue.
Table of contents, common errors, broken npm installation, random errors, no compatible version found, permissions errors, error: enoent, stat 'c:\users\<user>\appdata\roaming\npm' on windows 7, running a vagrant box on windows fails due to path length issues, npm only uses git: and ssh+git: urls for github repos, breaking proxies, ssl-intercepting proxy, not found / server error, invalid json, many enoent / enotempty errors in output, cb() never called when using shrinkwrapped dependencies, npm login errors, npm hangs on windows at addremotetarball, npm not running the latest version on a windows machine.
If your npm is broken:
- On Mac or Linux, reinstall npm .
- Windows: If you're on Windows and you have a broken installation, the easiest thing to do is to reinstall node from the official installer (see this note about installing the latest stable version ).
- Some strange issues can be resolved by simply running npm cache clean and trying again.
- If you are having trouble with npm install , use the -verbose option to see more details.
You have an outdated npm. Please update to the latest stable npm .
Please see the discussions in " Downloading and installing Node.js and npm " and " Resolving EACCES permissions errors when installing packages globally " for ways to avoid and resolve permissions errors.
The error Error: ENOENT, stat 'C:\Users\<user>\AppData\Roaming\npm' on Windows 7 is a consequence of joyent/node#8141 , and is an issue with the Node installer for Windows. The workaround is to ensure that C:\Users\<user>\AppData\Roaming\npm exists and is writable with your normal user account.
You are trying to install on a drive that either has no space, or has no permission to write.
- Free some disk space or
- Set the tmp folder somewhere with more space: npm config set tmp /path/to/big/drive/tmp or
- Build Node yourself and install it somewhere writable with lots of space.
You need to install git . Or, you may need to add your git information to your npm profile. You can do this from the command line or the website. For more information, see " Managing your profile settings ".
@drmyersii went through what sounds like a lot of painful trial and error to come up with a working solution involving Windows long paths and some custom Vagrant configuration:
This is the commit that I implemented it in , but I'll go ahead and post the main snippet of code here: config.vm.provider "virtualbox" do |v| v.customize ["sharedfolder", "add", :id, "--name", "www", "--hostpath", (("//?/" + File.dirname(__FILE__) + "/www").gsub("/","\\"))] end config.vm.provision :shell, inline: "mkdir /home/vagrant/www" config.vm.provision :shell, inline: "mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` > www /home/vagrant/www", run: "always" In the code above, I am appending \\?\ to the current directory absolute path. This will actually force the Windows API to allow an increase in the MAX_PATH variable (normally capped at 260). Read more about max path . This is happening during the sharedfolder creation which is intentionally handled by VBoxManage and not Vagrant's "synced_folder" method. The last bit is pretty self-explanatory; we create the new shared folder and then make sure it's mounted each time the machine is accessed or touched since Vagrant likes to reload its mounts/shared folders on each load.
@LaurentGoderre fixed this with some Git trickery :
I fixed this issue for several of my colleagues by running the following two commands: git config --global url. "https://github.com/" .insteadOf [email protected]: git config --global url. "https://" .insteadOf git:// One thing we noticed is that the .gitconfig used is not always the one expected so if you are on a machine that modified the home path to a shared drive, you need to ensure that your .gitconfig is the same on both your shared drive and in c:\users\[your user]\
You are trying to talk SSL to an unencrypted endpoint. More often than not, this is due to a proxy configuration error (see also this helpful, if dated, guide ). In this case, you do not want to disable strict-ssl – you may need to set up a CA / CA file for use with your proxy, but it's much better to take the time to figure that out than disabling SSL protection.
This problem will happen if you're running Node 0.6. Please upgrade to node 0.8 or above. See this post for details .
You could also try these workarounds: npm config set ca "" or npm config set strict-ssl false
npm no longer supports its self-signed certificates
- upgrade your version of npm npm install npm -g --ca=""
- tell your current version of npm to use known registrars npm config set ca=""
If this does not fix the problem, then you may have an SSL-intercepting proxy. (For example, https://github.com/npm/npm/issues/7439#issuecomment-76024878 )
Unsolved. See https://github.com/npm/npm/issues/9282
- It's most likely a temporary npm registry glitch. Check npm server status and try again later.
- If the error persists, perhaps the published package is corrupt. Contact the package owner and have them publish a new version of the package.
- Possible temporary npm registry glitch, or corrupted local server cache. Run npm cache clean and/or try again later.
- This can be caused by corporate proxies that give HTML responses to package.json requests. Check npm's proxy configuration .
- Check that it's not a problem with a package you're trying to install (e.g. invalid package.json ).
npm is written to use resources efficiently on install, and part of this is that it tries to do as many things concurrently as is practical. Sometimes this results in race conditions and other synchronization issues. As of npm 2.0.0, a very large number of these issues were addressed. If you see ENOENT lstat , ENOENT chmod , ENOTEMPTY unlink , or something similar in your log output, try updating npm to the latest version. If the problem persists, look at npm/npm#6043 and see if somebody has already discussed your issue.
Take a look at issue #5920 . We're working on fixing this one, but it's a fairly subtle race condition and it's taking us a little time. You might try moving your npm-shrinkwrap.json file out of the way until we have this fixed. This has been fixed in versions of npm newer than [email protected] , so update to npm@latest .
Sometimes npm login fails for no obvious reason. The first thing to do is to log in at https://www.npmjs.com/login and check that your e-mail address on npmjs.com matches the email address you are giving to npm login .
If that's not the problem, or if you are seeing the message "may not mix password_sha and pbkdf2" , then
- Log in at https://npmjs.com/
- Change password at https://npmjs.com/password – you can even "change" it to the same password
- Clear login-related fields from ~/.npmrc – e.g., by running sed -ie '/registry.npmjs.org/d' ~/.npmrc
and it generally seems to work.
See https://github.com/npm/npm/issues/6641#issuecomment-72984009 for the history of this issue.
Check if you have two temp directories set in your .npmrc :
Look for lines defining the tmp config variable. If you find more than one, remove all but one of them.
See https://github.com/npm/npm/issues/7590 for more about this unusual problem.
See the section about Windows here .