zoukankan      html  css  js  c++  java
  • NodeJS模块、包、NPM

    1.NodeJS模块

           每一个Nodejs都是一个NodeJS模块,包括JS文件,JSON文本文件,二进制模块文件。

           a.模块的应用               新建一个文件mytest.js,输入如下代码:

                   function hello() { console.log('Hello'); } function world() { console.log('World'); }

                   这就是一个NodeJS模块,但是怎么在其他模块中引入呢?我们需要为模块提供对外的接口,这就用到module.exports和exports.

                   我们可以这样写:

                         function hello() { console.log('Hello'); } 

                         function world() { console.log('World'); }

                         exports.hello=hello;

                         exports.world=world;

                    在其他模块引用时,可以使用require(module_name);载入需要的模块。

                    Eg: var hello=require('./mytest');或者var hello=require('./mytest.js');

                           hello.hello();

                           hello.world();

                     也可以这样写:

                           function Hello(){

                                  this.hello=function(){console.log('hello');};

                                  this.world=function(){console.log('world');};

                           }

                            module.exports=Hello;

                           引用时,就要写为 var hello=new Hello(); hello.hello(); hello.world();

            b.module.exports和exports

                 module是一个对象,每个模块中都有个module对象,module是当前模块的一个引用。module.exports对象时Modul系统 创建的,而exports可以看做是对module.exports对象的一个引用。在模块中require引用另一个模块时,以module.exports的值为标准,具体关系如下:

    // module.exports和exports相同的情况
    var m = {};        // 表示 module
    var e = m.e = {};  // e 表示 exports, m.e 表示 module.exports
    
    m.e.a = 5;
    e.b = 6;
    
    console.log(m.e);  // Object { a: 5, b: 6 }
    console.log(e);    // Object { a: 5, b: 6 }
    
    // module.exports和exports不同的情况
    var m = {};        // 表示 module
    var e = m.e = {};  // e 表示 exports, m.e 表示 module.exports
    
    m.e = { c: 9 };    // m.e(module.exports)引用的对象被改了
    e.d = 10;
    
    console.log(m.e);  // Object { c: 9 }
    console.log(e);    // Object { d: 10 }

    2.NodeJS包

         包用于管理多个模块以及依赖关系,可以对多个模块进行封装,包的根目录必须包含package.json文件,package.json文件是CommonJS规范用于描述包的文件,符合CommonJS规范的package.json文件应该包含以下字段:

      1.name:包名。包名是唯一的,只能包含小写字母,数字和下划线。

          2.version:包版本号。

          3.description:包说明。

          4.keywords:关键字数组。用于搜索。

          5.homepage:项目主页。

          6.bugs:提交bug的地址。

          7.license:许可证。

          8.maintainers:维护者数组。

          9.contributors:贡献者数组。

        10.repositories:项目仓储托管数组。

        11.dependencies:包依赖。

           { "name": "mytest",

      "description": "My test package.",

           "version": "0.1.0",

        "keywords": "mytest", "nodejs" ],

     "maintainers": [{ "name": "test", "email": "test@mytest.com" }],

    "contributors": [{ "name": "test", "web": "http://www.mytest.com/" }],

              "bugs": { "mail": "test@mytest.com", "web": "http://www.mytest.com/" },

          "licenses": [{ "type": "Apache License v2", "url": "http://www.apache.org/licenses/apache2.html" }],

      repositories": [{ "type": "git", "url": "http://github.com/test/test.git" }],

        "dependencies": { "webkit": "1.2", "ssl": { "gnutls": ["1.0", "2.0"], "openssl": "0.9.8" } } }

    3.npm包管理工具

         npm可以从第三方网站(http://www.npmjs.prg/)上下载第三方NodeJS包。

         搜索第三方包:

              sudo npm search express

         安装包:

              sudo npm install -g express

         更新包:

              sudo  npm update express

         卸载包:

              sudo npm uninstall express

     

     

     

  • 相关阅读:
    阿里妈妈又做了新工具,帮你把 Vue2 代码改成 Vue3 的
    gulp安装出错
    js操作select
    linux基本命令说明参数
    Linux基础——vim编辑器的使用
    常见报错——Uncaught TypeError: document.getElementsByClassName(...).addEventListener is not a function
    IE浏览器hack方法总结之条件注释判断
    浏览器兼容
    安好
    jQuery调整表列(左右拉动调整列宽)插件__colResizable,动态列如何使用
  • 原文地址:https://www.cnblogs.com/fang-beny/p/4779138.html
Copyright © 2011-2022 走看看