Could you use npm modules by using require() in phantomJS? It seems to be able to load javascript files so maybe this is possible?
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.
-
For example, chalk is not usable because it depends on process module.– Tien DoMar 2, 2016 at 3:57
-
1Can you provide an example, properly how to inject node module into phantomjs? Answer is obvious by how to make it work, require or injectjs? Feb 27, 2017 at 13:47
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.
require
, can you give me a code sample of what you'be tried?