修复casperjs中require自定义module传入绝对路径不能识别的问题

Notice:此问题只在windows中出现。

问题现象:

比如你在casperjs脚本中引入一个模块这样写:

var util = require("d:/my_modules/util");
....


执行时就会报找不到module,d:/my_modules/util.js

解决办法:

找到casperjs安装目录bin下面bootstrap.js,找到代码块:

function possiblePaths(path, requireDir) {
        var dir, paths = []; 
        if (path[0] === '.') {
            paths.push.apply(paths, [
                fs.absolute(path),
                fs.absolute(fs.pathJoin(requireDir, path))
            ]); 
        } else if (path[0] === '/') {
            paths.push(path);
        } else {
            dir = fs.absolute(requireDir);
            while (dir !== '' && dir.lastIndexOf(':') !== dir.length - 1) {
                paths.push(fs.pathJoin(dir, 'modules', path));
                // nodejs compatibility
                paths.push(fs.pathJoin(dir, 'node_modules', path));
                dir = fs.dirname(dir);
            }   
            paths.push(fs.pathJoin(requireDir, 'lib', path));
            paths.push(fs.pathJoin(requireDir, 'modules', path));
        }   
        return paths;
    }
将第二个
else if (path[0] === '/')    

改成

else if (path[0] === '/'|| (path.match(/^[A-Za-z]:/) && require('system').os.name=='windows'))


你可能感兴趣的:(casperjs)