Now we will introduce the fs
module which provides an API for working with the filesystem from a PhantomJS script. In this recipe, we will demonstrate how to save a file from PhantomJS and use the separator
property for generating filesystem-safe paths, checking whether the target directory exists, creating it if it does not, checking write permissions if it does, and then persisting the contents to the filesystem.
To run this recipe, we will need a script that expects to write a file to the filesystem. In order to write that file, we need write permissions in the destination directory.
The script in this recipe is available in the downloadable code repository as recipe08.js
under chapter02
. If we run the provided example script, we must change to the root directory for the book's sample code.