Nodejs explains the reading notes in depth (2)

CommonJS specification
-CommonJS has set a good vision for JavaScrip, is to hope that it can be used anywhere
a. No module system
b. Standard library less.
c. No standard interface
d. Lack of package manager
2. CommonJS module specification
a. Module reference

  var math = require('math')
  console.log(math.xxxx) 

b. Module definition
export.add = function ()
3. nodeJs module specification
a.Node finds files of .js, .json, .node in sequence
b. The way to install packages locally only needs to indicate the location of the package.json file for npm
4.
The scripts command in the npm hook package.json is to allow the package to provide a hook mechanism during installation or uninstallation
5. The release of the
npm package 1. npm login || npm adduser Then follow the prompts
2. Enter the folder to be uploaded , Execute npm publish in the same directory of the package.
Note: The mail sent by npm may be regarded as spam by the qq mailbox
. 6. Installation of
npm package npm install xxxx
7. Multi-person publishing package, add, delete, list
npm owner ls <package name>
npm owner add <user> <package name>
npm owner rm <user> <package name>
8. Analysis package
npm list
9. Problems with npm package platform
a. The level of the uploader is different, so the quality performance The aspect has yet to be verified
.
B. The package can run on the server side, and security issues need to be considered. 10. In view of the network, the specification specified by commonjs for server-side js does not apply to the front-end.
In this way, the AMD specification is generated. Asynchronous Module Definition is an
asynchronous definition module. It is a way to extend the
amd specification definition module of
define(function() { var exports = {}; exports.sayHello = function() { alert('Hello from module: ' + module.id); }; return exports; });
commondjs. 11. The cmd specification is mainly proposed by the domestic Yubo. The difference from the amd specification is mainly to define the module and introduce dependencies part.
amd needs to specify all the dependencies at the time of declaration. The dependencies are passed into the module content through formal parameters. The
define(['dep1', 'dep2'], function (dep1, dep2) { return function () {}; });
cmd specification is closer to the definition of the commonjs specification. cmd supports the dynamic introduction of dependencies.
define(factory); define(function(require, exports, module) { // The module code goes here });
12. Compatible with multiple module specifications
;(function (name, definition) { // 检测下文环境是否为AMD或CMD var hasDefine = typeof define === 'function', // 检查下文环境是否为Node hasExports = typeof module !== 'undefined' && module.exports; if (hasDefine) { // AMD环境或CMD环境 define(definition); } else if (hasExports) { // 定义为೵通Node模块 module.exports = definition(); } else { // 将模块的执行结果挂在window变量中,在浏览器中this指向window this[name] = definition(); } })('hello', function () { var hello = function () {}; return hello; });

Guess you like

Origin www.cnblogs.com/Nelsen8/p/12751015.html