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
NodeJS模块、包、NPM的更多相关文章
- Node.js 安装第三方模块包(npm),通过 package.json配置信息安装项目依赖的模块
npm下载安装的第三方模块包官网(提供包名和使用方法):https://www.npmjs.com/ 淘宝镜像(国内,比较快):https://npm.taobao.org/ commonjs01.j ...
- 前端笔记之NodeJS(二)路由&REPL&模块系统&npm
一.路由机制(静态资源文件处理) 1.1 Nodejs没有根目录 MIME类型:http://www.w3school.com.cn/media/media_mimeref.asp 在Apache中, ...
- 使用NodeJS模块-第三方提供的模块(什么是npm)
第三方开发者提供的模块 第三方模块是由NodeJS社区或第三方个人开发的功能模块,这些功能模块以软件包的形式存在.被发布在npmjs注册表中.npmjs是一个注册中心,所有软件包的信息都会被记录到该注 ...
- nodejs的包管理器npm和cnpm
http://www.ydcss.com/archives/18 3.npm介绍 3.1.说明:npm(node package manager)nodejs的包管理器,用于node插件管理(包括安装 ...
- NPM下载模块包说明
博主对npm包安装收集了各种资料和实践后对它们之间的差异整理,写下这篇文章避免自己忘记,同时也给node.js猿友一点指引. 我们在使用 npm install 安装模块的模块的时候 ,一般会使用下面 ...
- # nodejs模块学习: express 解析
# nodejs模块学习: express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子 ...
- Vue学习笔记之Nodejs中的NPM使用
0x00 NPM是什么 简单的说,npm就是JavaScript的包管理工具.类似Java语法中的maven,gradle,python中的pip. 0x01 NPM安装 傻瓜式的安装. 第一步:打开 ...
- nodejs模块学习: webpack
nodejs模块学习: webpack nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子来解决现实 ...
- NodeJS——模块全局安装路径配置以及关于supervisor的问题解释
下载安装NodeJS后,在自己选择的路径下会有如下的文件: 默认情况下NodeJS安装会同时安装npm(模块管理器:用于管理用户require的模块,有全局和本地两种). 注:全局:执行npm in ...
随机推荐
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- SQL1092N The requested command or operation failed because the user ID does not have the authority to perform the requested command or operation.
1.前一天安装号db2后,做了如下处理: ************************************************************ 修改 /etc/sudoers 文件 ...
- RMAN 报:ORA-19504 ORA-27038
在itpub中看到下面的问题: oracle 10g备份脚本如下run{allocate channel d1 device type disk MAXPIECESIZE=100M;crosschec ...
- Android--启动系统的剪切图像功能并返回结果
直接上代码: //启动裁剪图片 private void cropPhotoUri(Uri uri){ Intent intent = new Intent("com.android.cam ...
- core java 8~9(GUI & AWT事件处理机制)
MODULE 8 GUIs--------------------------------GUI中的包: java.awt.*; javax.swing.*; java.awt.event.*; 要求 ...
- Repair the database using DBCC CHECKDB
So now if you want to place AdventureWorks2008R2 sample database in a single-user mode, then write t ...
- Erlang generic standard behaviours -- gen
在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...
- Go语言类型switch
switch还可以用于判断变量类型.使用方式为T.(type),即在变量后加上.(type).见代码: package main import ( "fmt" ) func mai ...
- linux php安装zookeeper扩展
linux php安装zookeeper扩展 tags:php zookeeper linux ext 前言: zookeeper提供很犀利的命名服务,并且集群操作具有原子性,所以在我的多个项目中被采 ...
- IOS判断网络环境
https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html 我下载的是vertio ...