'Can't find module fs' when running ca

2019-05-29 08:25发布

问题:

I am trying to run CasperJS on a javascript file that I have, and when I run

> casperjs index.js 

it says

'CasperError: Cant find module fs'. 

The first line of index.js is

var fs = require('fs');

I have node, casperjs, and phantomjs installed, why can't it find fs? If I am correct, fs is Node's file system?

UPDATE: I added nodejs to my $PATH, but still no luck.

回答1:

Did you install the PhantomJS/CasperJS modules as well as the node fs module in your project? They don't seem to mix very well.

I ran into an issue where installed all of these modules and I was getting the same error message you mentioned above.

CasperError: Can't find module fs

Once I uninstalled the node 'fs' module, everything worked fine.

npm uninstall fs


回答2:

Are casperjs and phantomjs installed globally ?



回答3:

CasperJS is depending on PhantomJS, which have a built-in fs module. In Casper script, you are only able to use the phantomjs built-in fs module. See http://phantomjs.org/api/fs/

To solve the problem you describe, you will need to make sure there is no fsmodule defined in the package.json, or run npm uninstall fs