如何创建NodeJS模块?

Ami*_*ein 6 node.js

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

我该如何创建模块?
我如何使用模块?
把它放在npm上意味着什么?

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

Ami*_*ein 14

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

//mymodule.js

module.exports = 3; 
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用require加载模块:

//app.js

require('./mymodule.js')
Run Code Online (Sandbox Code Playgroud)

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

现在,如果您这样做:

var mymodule = require('./mymodule.js');
console.log(mymodule); // 3
Run Code Online (Sandbox Code Playgroud)

您可以使用函数替换数字3,例如:

//mymodule.js:
module.exports = function () {
  console.log('function inside the module');
};
Run Code Online (Sandbox Code Playgroud)

然后你可以使用它:

var mymodule = require('./mymodule.js');
mymodule();
Run Code Online (Sandbox Code Playgroud)

私人变量:

您在A模块中定义的每个变量都只在其中定义:

//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
Run Code Online (Sandbox Code Playgroud)

重用模块:

您需要了解的有关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 
Run Code Online (Sandbox Code Playgroud)

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

一个模块包

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

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

的package.json

{
  "name" : "You module name",
  "version" : "0.0.3"
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用发布模块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;
Run Code Online (Sandbox Code Playgroud)

更多信息:

相关问题:


小智 5

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

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

function display(){
    console.log('i am in a display function');
    }
module.exports = display;
Run Code Online (Sandbox Code Playgroud)

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

var display = require('./functional');
display()
Run Code Online (Sandbox Code Playgroud)

输出:我在显示功能中

同样你可以这样做:

var exports = module.exports = {};
exports.display = function(){
    console.log('i am in the display function');
}
Run Code Online (Sandbox Code Playgroud)

或者您对以下对象执行相同操作:

var funObj = {
    hello:function(){
            console.log('hello function');
          },
    display:function(){
            console.log('display function');
          }
};
module.exports = funObj;
Run Code Online (Sandbox Code Playgroud)