PhantomJS require() a relative path

后端 未结 5 529
情书的邮戳
情书的邮戳 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: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();
    

提交回复
热议问题