NODEJS(9)Book Read - hands-on-nodejs.pdf - Module and NPM
1. Starting up from Node and NPM
NPM - Node Package Manager
Global vs. Local
-g is the difference between this 2 mods. Local is the default mode, and it will keep the modules in node_modules directory.
npm Command
Query and list the modules
>npm ls
>npm ls [filter name]
Install and Remove
>npm install [package name] or >npm install -g [package name]
>npm rm [package name] or >npm rm -g [package name]
Check the Version from Latest Remote
>npm info express version
View all the info local
>nom view express
2. Understanding Module
CommonJS/AMD are the standard to modular the functions.
2.1. Object Literals
var myObject = {
variableKey: variableValue,
functionKey: function() {
}
}
IIFE Immediately-Invoked Function Expressions
( function(){/*code*/}() );
I do not quite understand this, I will leave it for future.
Import the global variable for IIFE
var myModule = (function(JQ) {
function method1(){
JQ(“.container”).html(“test”);
}
return{
publicMethod: function(){
method1();
}
};
})(JQuery);
myModule.publicMethod();
Module Export
return the object will do.
var myModule = (function(){
var module = {}, privateVariable = “design”;
function privateMethod(){
//…snip...
}
module.publicProperty = “designStudio”;
module.publicMethod = function (){
console.log(privateVariable);
}
return module;
})();
Expand the Module
var myModule = (function (my){
my.xxMethod = function (){
…snip...
}
})(myModule);
The only concern is that we need to have myModule first.
var myModule = (function(my){
my.xxMethod = function(){
…snip...
}
})(myModule || {});
The other concern is that, we may define myModule in a.js and also in b.js, they may have conflict.
(function(my){
my.xxMethod = function(){
}
})(window.myModule = window.myModule || {});
2.2 CommonJS
global method require() it can load the module, after this command, we can call all the functions belongs to that module.
for example>
var math = require(‘math’);
math.add(2, 3);
global variable exports, once one method assign to this variable, it become the public function of that module.
for example>
//math.js
exports.add = function(){
…snip...
};
2.3 AMD Standard
AMD - Asynchronous Module Definition
2.4 the way node.js do
var module = require(‘module_name’);
This will fetch a module that was installed by nom.
var module = require(“./path/to/module_name”);
This will fetch the module from a path relative to the current file.
2.5. How Node resolves a module path
Core modules ——> Module with complete or relative path (“./“ or “/")——> Load the file ———> try append “.js”———> try append “.node”
———> if appending “/package.json”, try loading the package definition and looking for ‘main’ ———> no “.”, no “/“, then it will try to find them in /node_modules
3. Making Modules
One File Module
Sample to export only one public method as module definition.
lib/index.js
var counter = 0;
var onePrivateMethod = function(){
return counter;
};
var onePublicMethod = function(){
console.log("you already called this module " + onePrivateMethod() + " times.");
};
module.exports = onePublicMethod;
index.js
var myModule = require('./sillycat-aes/lib/index');
myModule();
When we directly call myModule(); my understanding it is a immediately execution function.
>node index.js
you already called this module 0 times.
Exports a JavaScript Object
Actually, an JavaScript Object is a collection of functions and key value pairs, I can export one object as follow>
var counter1 = 0;
var onePublicMethod = function(){
return 'you already called this function ' + (++counter1) + ' times';
};
var counter2 = 0;
var anotherPublicMethod = function(){
return 'you already called this function ' + (++counter2) + ' times.';
};
module.exports = {
functionA: onePublicMethod,
functionB: anotherPublicMethod
};
That is the file provide us functions and objects. The main app who can call all the methods is as follow:
var myModule = require('./sillycat-aes/lib/index');
console.log(myModule.functionA());
console.log(myModule.functionA());
console.log(myModule.functionB());
Here is the console output>
node index.js you already called this function 1 times you already called this function 2 times you already called this function 1 times.
An aggregating module
Put some other module inside yours, expose their methods.
var moduleA = require(‘./moduleA’);
var moduleB = require(‘./moduleB’);
var myFunc = function(){
return “doing some stuff”;
}
module.exports = {
funcA:moduleB.funcA,
funcB:moduleB.funcB,
funcC:myFunc
}
References:
The Node. Beginner Book.pdf
Example to Build Web
http://keystonejs.com/getting-started/
http://yeoman.io/
CommonJS and AMD
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-basics
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-commonjs
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-amd
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-requirejs
http://www.feeldesignstudio.com/2013/10/javascript-module-pattern-further-reading
How to configure package.json
http://docs.spmjs.org/doc/package
http://blog.uifanr.com/2013/03/13/478
http://blog.csdn.net/zimin1985/article/details/18958741
- 浏览: 2567331 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 492NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 351Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 449Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 401Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 496NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 438Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 346Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 262GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 463GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 336GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 322Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 330Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 320Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 307NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 272Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 585NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 279Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 388Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 387Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
nodejs010-nodejs-npm-registry-client-0.3.3-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-npm-user-validate-0.0.3-2.1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-fstream-npm-0.1.6-1.el6.centos.alt.noarch.rpm
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-npm-1.3.24-5.el6.centos.alt.noarch.rpm
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-nodejs-read-1.0.5-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-read-installed-0.2.4-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-read-package-json-1.1.6-1.el6.centos.alt.noarch.rpm
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-nodejs-nopt-2.1.2-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-lockfile-0.4.2-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-asn1-0.1.11-3.1.el6.centos.alt.noarch.rpm
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
debain安装nodejs和npm,nodejs12.18.2-npm6.14.5版本,二进制编译好的,直接可以使用
nodejs010-nodejs-chownr-0.0.1-9.el6.centos.alt.noarch.rpm
# node -v v10.24.1 # npm -v 6.14.12
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装