In this recipe, we will learn how to load custom modules in our PhantomJS scripts using the require
function. PhantomJS has several built-in modules, but we can also write our own (see the Creating a custom module for PhantomJS recipe earlier in this chapter) and import them in this way.
To run this recipe, we will need a custom module that we want to import using the require
function; we can use the arg-parser.js
module that we wrote in the Creating a custom module for PhantomJS recipe earlier in this chapter.
The script in this recipe is available in the downloadable code repository as recipe11.js
under chapter02
. If we run the provided example script, we must change to the root directory for the book's sample code.