Node Module Introduction

A, Node modular development needs

concept

Before ES6

There are several issues ECMAScript

  1. No module system
  2. Less official standard library / less standard interface
  3. The lack of package management system

Modular

  1. If the program is designed to scale to a certain extent, it must be modular
    front-end not much need, so he did not need
    server-side development, no thought could not handle the modular development
  2. Modular may take many forms, but provides a mechanism can be divided into a plurality of source code files
    Common.js

CommonJS specification

  1. CommonJS norms proposed, mainly to compensate for JavaScript no standard modular defects
  2. JS JS hope CommonJS specification can run anywhere: This is a wonderful vision
  3. CommonJS module definition
    module references: require ( "path");
    module definition
    module identification

to sum up

  1. From the perspective of files, each file is a JS module
  2. In terms of structure, between multiple JS files may require each other to achieve a common feature, this is a whole module
  3. In Node.js, one module defined variables, functions, etc., are only effective within this document
  4. When required an external JS file references from these variables, function calls, exports must be exposed, require the user by reference

Practice 1

  1. In Node, one js file is a module

  2. In Node by function modules require the introduction of an external ()
    introduced into the external module to be added. Alternatively ...

  3. In Node, each js file js code is run independently in a small closure, rather than global scope, so the variables and functions in one module can not access other modules
    Objective: Privatization of global variables avoid global pollution

  4. Exposure variables and function module
    export
    variable or method need only need to expose to the outside can be set as the attribute of exports

    02.js

    let site = 'www.itlike.com';
    
    let log = ()=>{
       console.log('这是一个神奇的网站:', site);
    };
    
    exports.site = site;
    exports.log = log;
    

    03.js

    // 1. 引入其它模块
    let  fn = require('./02.js');
    // console.log(fn);
    console.log(fn.site);
    fn.log();
    
    

Practice 2

Module identification

  1. When we use require () introduced into the external module, the identification module is used, we can find the specified module through the module identifier
    such as: let myFunc = require ( "./ js / myFunc");
  2. Classification
    ① built-in modules: the underlying written by C ++
    ② file modules: a module users to create their own
    identity: a path (absolute path, relative path) file
    ③ core modules: Module provided by the node engine module provided by node_modules
    ID: name of the module, HTTP, FS,, Ltd. Free Join ...
    ...

Thinking: export and require how come?

  1. Incorrect Answers: global variables
    global object 1) window is not in the Node
    2) Node has a global object global, similar to the role and window
  2. The correct answer: function parameter
    1) identifies the function: arguments
    get the function of all the arguments
    2) get their arguments.callee function
    returns the function itself

node analysis files

  1. When a node in the code module execution, it first at the very top of the code, add the following code function (exports, require, module, __filename, __dirname) {
  2. At the bottom of the code, add}
  3. Therefore, the module code is executed in a function package, and the transfer function performed at the same time into five arguments
    ① exports: This object is used to expose local variables inside a function or to an external local functions
    ② require: for introducing external module
    ③ module: represents the current module itself, the module exports that property; we can either use exports to export, can also be used module.exports export
    ④ __filename: current full path to the module
    ⑤ __dirname: where the current module the full path to the folder

Node in Common.js

  1. In node.js, the module division all functions, each module is a JS

  2. Require realization method, NPM and to achieve automatic load-dependent modules installed

  3. Internal form

    (function(exports,require,module,__filename,__dirname){
      exports = module.exports={}
      exports.brands = 'itlike';
      exports = {brands :'itlike'};
      return module.exports;
    })
    

The difference between exports and module.exports

Difference between the two

  1. In each module, the ultimate return of module.exports
    (function(exports,require,module,__filename,__dirname){
        exports = module.exports={};
    
    
        return module.exports;
    })
    
    So the module to change the way are not affected, but exports will not return the correct change.
  2. exports only syntax outwardly to expose internal variables.
    exports.xxx = XXX;
  3. module.exports either by syntax may be assigned to an object directly.
    module.exports.xxx = yyy;
    module.exports = {XXX: yyy};

Value types and reference types

二、module&NPM

Classification Module

Native module

http path fs util events compiled into a binary, load the fastest, native module is to load by name

File module

  1. Somewhere hard disk, loading very slow to load module file by name or path
  2. There are three file extension module
    ① JavaScript script file named .js suffix, you need to read into memory and then run
    ② suffix .json a JSON file, it needs fs module is read into memory, transformed into a JSON object
    ③ suffix. after .node the compiled binary C / C ++ file extension module can be used directly
  3. Note
    ① general write their own path through the load
    ② written by others by name to the current directory or to look for the following global node_modules

Third party modules

  1. If you require only the specified function name, is considered the path from the module below node_modules load the file, so we can move the module without the need to modify the reference
  2. Query path third-party modules include module.paths and global catalog
  3. Global Directory
    ① window when setting up NODE_PATH variables in the environment variable, and the variable is set to a valid disk directory, require this module can not be found locally, will continue upward in this directory to find this module
    ② UNIX operating system will look from under $ HOME / .node_modules $ HOME / .node_libraries directory
  4. Module loading strategy
    Here Insert Picture Description

package

  1. In Node.js can be unified management module has a set of interdependence by coating, can be encapsulated by an independent function package
  2. The root of each of the following items, in general, a package.json file, define this project required various modules, and configuration information items (such as name, version, license and other metadata)
  3. Use npm initcommand to initialize package
  4. npm installAn order under this configuration file, the module automatically downloads the required configuration is required to run the project and development environment
  5. Icon
    Here Insert Picture Description

NPM

concept

  1. Use only Node core language features and functions after installing node, we also need a system to download, install and manage third-party modules.
  2. See in this system is called Node Node Package Manager (Node Package Manager, NPM)

npm functionality provided

  1. Public registration service, users can write their own package uploaded to the server
  2. Command-line download tool, users can write command npm package to someone else on your computer, you can also manage their own modules rely on other modules
  3. Address Search third-party package: https: //www.npmjs.com/search

npm command

  1. Installation package
    open a command line or terminal, enter the directory you want to install the package, and then execute the following command to install the dependent modules
    npm install <package-name>
    npm i jquery
    This command will download the module from the server to the current directory of node_modules, if node_modules directory does not exist it will be created , you can also install a specific version
    npm install <package name>@<version spec>
    npm i [email protected]
    npm i [email protected]
  2. Uninstall package
    npm uninstall <package name>
  3. Update package
    npm update <package name>

Package installation mode

  1. Local Installation
    ① default installation will command corresponding to the case where the package to the current directory, called local mounting
    ② If the package contains an executable file, the executable file of NPM will be installed to ./node_modules/.bin directory
    ③ locally installed module can only be used in the current directory and subdirectories inside the current directory
  2. Global Installation
    ① want to install the package can be able to use all of the following directory on the computer machine, you need to install global
    npm install <package-name> -g
    ② in global installation mode, npm will install the package to the global directory, use this command to view the current global directory location
    Here Insert Picture Description
    ③ If you want to modify global installation directory, you can use
    npm config set prefix “E:\node.js\node_global”
  3. Register, login and publish modules
    registered npm https://www.npmjs.com/ account
    login, npm login
    released, npm publish

yarn

concept

  1. yarn is a dependency management tool
  2. It can manage your code and share the code with developers around the world
  3. Codes by packet (sometimes referred to as module) sharing
  4. In each package contains all the necessary shared code, also defines a package.json file that describes the package

Installation yarn

npm yarn --save -g

Initialize a new project

yarn init

Add a dependency package

yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]

Update a dependent package

yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]

To delete a dependent package

yarn remove [package]

Install all dependencies

yarn
yarn install

Published 297 original articles · won praise 128 · views 20000 +

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/104984106