How do I call .js from another .js file?

This question already has an answer here:

  • In Node.js, how do I “include” functions from my other files? 20 answers

  • You can create a module, and require it the following way.

    File A: sql.js

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

    Files B, C, D:

    var sql = require("./sql");
    
    sql.a();
    

    require .

    for example var sql = require('sql.js');

    you need in the sql.js to return an object at the end with module.exports = myobj ;

    Example:

    module.exports = {
     sql_connection: null,
     connect: function() {
        // connect to db
       this.sql_connection = ... ; // code to connect to the db
     }
    
    };
    
    链接地址: http://www.djcxy.com/p/97012.html

    上一篇: NodeJS替代PHP包括?

    下一篇: 如何从另一个.js文件调用.js?