Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phantomjs: How to use npm modules in phantomjs?

Tags:

npm

phantomjs

Could you use npm modules by using require() in phantomJS? It seems to be able to load javascript files so maybe this is possible?

like image 934
KJW Avatar asked Sep 25 '13 05:09

KJW


People also ask

Is PhantomJS deprecated?

PhantomJS is a discontinued headless browser used for automating web page interaction.

How do I run PhantomJS?

For Windows Download the zip file, unpack it and you will get an executable phantom.exe. Set the PATH environment variable to the path of phantom.exe file. Open a new command prompt and type phantomjs –v. It should give you the current version of PhantomJS that is running.


2 Answers

That is entirely depends on the nodejs module. It won't work if nodejs module requires some support via native function interface.(npm modules like http, socket.io, will not work on phantomjs, because it dependents on native interfaces)

Pure JavaScript nodejs modules should work on phantomjs too.

like image 175
Arunprasad Rajkumar Avatar answered Dec 25 '22 20:12

Arunprasad Rajkumar


fyi, phantomjs has an aproximation of commonjs.

you can not use amd modules in phantomjs, and I do not know of any amd loader (like requirejs) that works in phantomjs

Update (2015): I have been using commonjs modules in PhantomJs 1.x and 2.x, they work well with one caveat: you can not use source-mapping (at the bottom of the file, looks like //# sourceMappingURL=index.js.map) as it will cause the PhantomJs module loader to fail. Otherwise, commonjs modules will load normally.

like image 40
JasonS Avatar answered Dec 25 '22 20:12

JasonS