【问题标题】:How Do I create a NodeJS Module?如何创建 NodeJS 模块?
【发布时间】:2016-06-23 15:19:21
【问题描述】:

我已阅读 NodeJS 网站上的详细信息:https://nodejs.org/api/modules.html。我不明白模块是如何工作的,创建模块的最小步骤是什么,以及 npm 可以如何帮助我。

如何创建模块?
如何使用模块?
放在 npm 上是什么意思?

注意:这是一个自我回答的问题,目的是作为规范分享知识。

【问题讨论】:

    标签: node.js


    【解决方案1】:

    在 node.js 中创建模块非常简单!!!

    您可以将模块视为一组可以在其他代码中使用的功能,只需简单地要求它即可。 例如:考虑具有以下内容的文件functional.js:

    function display(){
        console.log('i am in a display function');
        }
    module.exports = display;
    

    现在只需要在任何其他模块中使用它,例如:

    var display = require('./functional');
    display()
    

    输出:我在显示函数中

    你也可以这样做:

    var exports = module.exports = {};
    exports.display = function(){
        console.log('i am in the display function');
    }
    

    或者你对像这样的对象做同样的事情:

    var funObj = {
        hello:function(){
                console.log('hello function');
              },
        display:function(){
                console.log('display function');
              }
    };
    module.exports = funObj;
    

    【讨论】:

      【解决方案2】:

      您可以使用一行代码创建一个 NodeJS 模块:

      //mymodule.js
      
      module.exports = 3; 
      

      然后你可以使用 require 来加载模块:

      //app.js
      
      require('./mymodule.js')
      

      我添加了“./”,因为它是一个文件的模块。我们稍后会介绍。

      现在如果你这样做:

      var mymodule = require('./mymodule.js');
      console.log(mymodule); // 3
      

      您可以将数字 3 替换为函数,例如:

      //mymodule.js:
      module.exports = function () {
        console.log('function inside the module');
      };
      

      然后就可以使用了:

      var mymodule = require('./mymodule.js');
      mymodule();
      

      私有变量:

      您在模块中定义的每个变量都将仅在其内部定义:

      //mymodule.js
      var myPrivateVariable = 3;
      publicVariable = 5;   // Never user global variables in modules
                            //It's bad-pracrtice. Always add: var.
      module.exports = function() {
        // Every function of the module can use the private variables
        return myPrivateVariable++
      };
      
      //app.js
      
      var mymodule = require('./mymodule.js');
      console.log(mymodule()); // return 3
      console.log(mymodule());  // return 4
      

      重用模块:

      关于 NodeJS 模块你需要了解的另一件事是,如果你两次使用同一个模块(需要它),它将返回相同的实例,它不会运行两次。

      例如:

      //app.js
      
      var mymodule1 = require('./mymodule.js');
      var mymodule2 = require('./mymodule.js');
      console.log(mymodule1()); //return 3
      console.log(mymodule2()); //return 4 (not 3)
      console.log(mymodule1()); //return 5 
      

      正如您在下面的示例中看到的,该私有变量在模块的所有实例之间共享。

      一个模块包

      如果您的模块包含多个文件,或者您想与其他人共享该模块,您必须在单独的文件夹中创建该模块,并为该模块创建一个package.json 文件。

      npm init 将为您创建 package.json 文件。 对于模块,有 3 个必需部分:

      package.json

      {
        "name" : "You module name",
        "version" : "0.0.3"
      }
      

      现在,您可以使用 npm publish 发布模块。我建议您也将所有模块发布到 github,然后该模块将连接到您的 github 页面。

      每个人都可以访问您发布到 NPM 的内容。所以永远不要发布包含私有数据的模块。为此,您可以使用私有 npm 模块。

      后续步骤

      模块可以返回多个函数或一个变量。请参阅我们返回对象的示例。

      module.exports.a = function() {
        // ..
      };
      module.exports.b = function() {
        // ..
      };
        
      // OR
      
      myObj = {
        a:3,
        b:function() {
          return this.a;
        }
      };
      module.exports = myObj;
      

      更多信息:

      相关问题:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-09
        • 2020-06-08
        • 2013-09-25
        • 2014-01-02
        • 1970-01-01
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多