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

后端 未结 25 2555
猫巷女王i
猫巷女王i 2020-11-22 04:22

Let\'s say I have a file called app.js. Pretty simple:

var express = require(\'express\');
var app = express.createServer();
app.set(\'views\', __dirname + \         


        
相关标签:
25条回答
  • 2020-11-22 05:03

    create two files e.g app.js and tools.js

    app.js

    const tools= require("./tools.js")
    
    
    var x = tools.add(4,2) ;
    
    var y = tools.subtract(4,2);
    
    
    console.log(x);
    console.log(y);
    

    tools.js

     const add = function(x, y){
            return x+y;
        }
     const subtract = function(x, y){
                return x-y;
        }
    
        module.exports ={
            add,subtract
        }
    

    output

    6
    2
    
    0 讨论(0)
  • 2020-11-22 05:03

    Like you are having a file abc.txt and many more?

    Create 2 files: fileread.js and fetchingfile.js, then in fileread.js write this code:

    function fileread(filename) {
        var contents= fs.readFileSync(filename);
            return contents;
        }
    
        var fs = require("fs");  // file system
    
        //var data = fileread("abc.txt");
        module.exports.fileread = fileread;
        //data.say();
        //console.log(data.toString());
    }
    

    In fetchingfile.js write this code:

    function myerror(){
        console.log("Hey need some help");
        console.log("type file=abc.txt");
    }
    
    var ags = require("minimist")(process.argv.slice(2), { string: "file" });
    if(ags.help || !ags.file) {
        myerror();
        process.exit(1);
    }
    var hello = require("./fileread.js");
    var data = hello.fileread(ags.file);  // importing module here 
    console.log(data.toString());
    

    Now, in a terminal: $ node fetchingfile.js --file=abc.txt

    You are passing the file name as an argument, moreover include all files in readfile.js instead of passing it.

    Thanks

    0 讨论(0)
  • 2020-11-22 05:04

    app.js

    let { func_name } = require('path_to_tools.js');
    func_name();    //function calling
    

    tools.js

    let func_name = function() {
        ...
        //function body
        ...
    };
    
    module.exports = { func_name };
    
    0 讨论(0)
  • 2020-11-22 05:05

    You can require any js file, you just need to declare what you want to expose.

    // tools.js
    // ========
    module.exports = {
      foo: function () {
        // whatever
      },
      bar: function () {
        // whatever
      }
    };
    
    var zemba = function () {
    }
    

    And in your app file:

    // app.js
    // ======
    var tools = require('./tools');
    console.log(typeof tools.foo); // => 'function'
    console.log(typeof tools.bar); // => 'function'
    console.log(typeof tools.zemba); // => undefined
    
    0 讨论(0)
  • 2020-11-22 05:05

    It worked with me like the following....

    Lib1.js

    //Any other private code here 
    
    // Code you want to export
    exports.function1 = function(params) {.......};
    exports.function2 = function(params) {.......};
    
    // Again any private code
    

    now in the Main.js file you need to include Lib1.js

    var mylib = requires('lib1.js');
    mylib.function1(params);
    mylib.function2(params);
    

    Please remember to put the Lib1.js in node_modules folder.

    0 讨论(0)
  • 2020-11-22 05:08

    Create two js files

    // File cal.js
    module.exports = {
        sum: function(a,b) {
            return a+b
        },
        multiply: function(a,b) {
            return a*b
        }
    };
    

    Main js file

    // File app.js
    var tools = require("./cal.js");
    var value = tools.sum(10,20);
    console.log("Value: "+value);
    

    Console Output

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