1

FileUtil.js:

exports.a = function(pre) {
    var module = {}

    module.writeStringToFile = function writeStringToFile() {
        casper.log(pre + " succ");
    };

    return module
}

main.js:

var casper = require('casper').create();
var FileUtil = require('FileUtil').a('xxx')
FileUtil.writeStringToFile() //xxx succ

That works, but what I want is var FileUtil = require('FileUtil')('xxx') instead of require('FileUtil').a('xxx').

I tried exports = function(pre) ..., but it doesn't works.

So, how to make a custom CasperJS module with custom parameter?

1 Answer 1

2

If you want var FileUtil = require('FileUtil')('xxx') to be your object then you need to use module.exports. It can export a single object, which can even be a function:

module.exports = function(pre) {
    var module = {}

    module.writeStringToFile = function writeStringToFile() {
        casper.log(pre + " succ");
    };

    return module
}

Of course, it would be a better form to rename the inner module variable to something else.

Sign up to request clarification or add additional context in comments.

2 Comments

I am a little bit curious...Why all of the official library of CasperJS (such as util.js) use exports instead of module.exports? What's the different between exports and module.exports?
No idea. Perhaps it's personal preference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.