PhantomJS require() a relative path

后端 未结 5 528
情书的邮戳
情书的邮戳 2021-02-04 03:07

In a PhantomJS script I would like to load a custom module but it seems relative paths do not works in PhantomJS ?

script.js:

var foo = require(\'./scrip         


        
相关标签:
5条回答
  • 2021-02-04 03:50

    My solution to load a resource file (like let's say a json file) within a phantomjs subfolder from a outer folder like in this structure:

    ├── consumer.js
    ├── assets
        ├── data.json
        ├── loader.js
    

    Supposed that data.json must be load by the consumer module and that this module is called by somewhere else on this machine, outside the project root folder, the fs.workingDirectory will not work, since it will be the path of the caller file. So to solve this, I did a simple loader module within the assets folder, where the files I want to load are:

    (function() {
    
    var loader = {
        load : function(fileName) {
            var res=require('./'+fileName);
            return res;
        }
    }
    module.exports=loader;
    
    }).call(this);
    

    I therefore call the loader module from the consumer module like

    var loader=require('./data/loader');
    var assets=loader.load('data.json');
    

    and that's it.

    NOTE. The require here is the phantomjs require not the node version, so it works a bit differently. In this case the data.json was a json array with no module.exports declaration. The array will be backed in the assets variable directly when calling the loader.load(fileName) method.

    0 讨论(0)
  • 2021-02-04 03:52

    To load your own module, the right way to do it is to use module.exports, like this: foo.js

    function bar(text) {
       console.log(text);
    }
    
    exports.bar = bar 
    

    And in script.js (which is executed with phantomjs script.js):

    var foo = require('./script/lib/foo');
    foo.bar('hello world');
    phantom.exit();
    
    0 讨论(0)
  • 2021-02-04 03:55

    have you tried to use injectJs(filename)

    excerpt form PhantomJS documentation:

    Injects external script code from the specified file. If the file can not be found in the current directory, libraryPath is used for additional look up.

    This function returns true if injection is successful, otherwise it returns false.

    0 讨论(0)
  • 2021-02-04 03:57

    After a lot of time searching for the same thing, here is what I understood, though I might be wrong :

    • PhantomJS doesn't use Node's require, but its own require, so things are different
    • when providing a relative path to phantomjs's require, it is always interpreted as relative to the current working directory
    • PhantomJS doesn't implement node's __dirname, and as such there is no direct way to have the directory of your script

    The solution I found least annoying :

    • if using phantomjs pure, without casperjs :

      require(phantom.libraryPath + '/script/lib/foo.js')
      
    • if using casperjs :

      var scriptName = fs.absolute( require("system").args[3] );
      var scriptDirectory = scriptName.substring(0, scriptName.lastIndexOf('/'));
      require(scriptDirectory + '/script/lib/foo.js')
      
    0 讨论(0)
  • 2021-02-04 04:10

    Which PhantomJS version are you running? Support for user provided modules was added in 1.7.

    0 讨论(0)
提交回复
热议问题