In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand the package and NPM in Node.js". In the daily operation, I believe many people have doubts about how to understand the package and NPM in Node.js. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to understand the package and NPM in Node.js"! Next, please follow the editor to study!
Abstract: the package and NPM Node organize their own core modules, which also enable the third-party file modules to be written and used in an orderly manner.
Package and NPM
Node organizes its own core modules and enables third-party file modules to be written and used in an orderly manner.
However, in the third-party module, the module and the module are still scattered everywhere, and can not be directly referenced from each other.
So outside the module, the package and NPM are the mechanisms that connect the module.
Schematic diagram of package organization module
The package specification definition of CommonJS is also very simple, which consists of two parts: the package structure and the package description file.
Packet structure
Used to organize the various files in the package, it is an archive file, that is, a directory that is packaged directly into .zip or tar.gz format.
Package directories that conform to the specification:
Package.json: package description file
Bin: the directory where executable binaries are stored
Lib: the directory where the JavaScript code is stored
Doc: the directory where documents are stored
Test: code for storing unit test cases
Package.json package description file
All behaviors of NPM are closely related to the fields of the package description file
Some fields:
Name: package name. The specification definition needs to be made up of lowercase letters and numbers, and spaces are not allowed. The package name must be unique to avoid duplicate name conflicts when it is made public.
Description: introduction to the package
Version: version number, which is also mentioned in "Node.js Learning (1)-introduction".
Keywords: keyword array, mainly used for classified search in NPM.
Maintainers: list of package maintainers. Each maintainer consists of three attributes: name, email, and web. NPM authenticates permissions through this property.
Format:
"maintainers": [{"name": "kongchengji", "email": "111@.com", "web": "[http:] (https://blog.csdn.net/qq_36171287)"}]]
Contributors: list of contributors in the same format as the maintainer list
Bugs: a web address or email address that can feed back to bug
Licenses: list of licenses used by the current package, indicating under which licenses the package is used
Format:
"licenses": [{"type": "GPLv2", "url": "}] / / or" license ":" ISC "
Repositories: a list of locations for managed source code, indicating how and at which addresses the package source code can be accessed.
Format:
"repository": {"type": "git", "url": "git+ https://github.com/kongchengji/UiSelfMade.git"},"
Dependencies: a list of packages that you need to rely on to use the current package. This attribute is very important.
Homepage: the website address of the current package
Os: operating system support list. If the list is empty, no assumptions are made about the operating system.
Cpi: CPU schema support list
Engine: list of supported JavaScript engines
Directories: package directory description
Implements: a list of implementation specifications. Marks which specifications of CommonJS are implemented by the current package
Scripts: the script describes the object. Mainly used by the package manager to install, compile, test and uninstall packages
Format:
"scripts": {"dev": "webpack-dev-server-- inline-- progress-- config build/webpack.dev.conf.js", "start": "npm run dev", "lint": "eslint-- ext .js, .vue src", "build": "node build/build.js"}
NPM differs from the package specification in that there are four more fields:
Author: package author: ok_man:
Bin: some package authors want packages to be used as command-line tools.
Main: when a module introduces a package, it will check this field limited and use it as an entry module for the rest of the module in the package. If it does not exist, require will look for index.js, index.node, and index.json in the package directory as default entries
DevDependencies: some modules need to be relied on only at development time.
DevDependencies: development environment uses dependencies: production environment uses
Front and rear common module
With the advent of Node, JavaScript has one advantage-> some modules can be shared at the front and back end.
But there are always some differences between the front and rear ends: sweat_drops:
Focus on front and rear modules
The front and rear JavaScript are shelved at both ends of the HTPP and play different roles.
The JavaScript on the browser side needs to be distributed from the same server to multiple clients for execution. The bottleneck is bandwidth. The JavaScript on the server side loads the code from the network. The same code needs to be executed multiple times. The bottleneck is that resources such as CPU and memory are loaded from disk.
In the front-end JavaScript, the AMD specification is mainly applied.
CommonJS is not completely suitable for front-end JavaScript, for example, the module introduction of Node is basically synchronous, but if the front-end introduction is introduced using synchronization, UI needs to spend a lot of time waiting for the script to be loaded during initialization.
AMD specification
AMD specification is an extension of CommonJS specification, full name: Asynchronous Module Definition.
Is an asynchronous module definition
Module definition: define (id?,dependencies?, factory)
Id is the name of the module, which is an optional parameter.
Dependencies specifies the list of modules to depend on, which is an array and an optional parameter
AMD needs to specify all dependencies when declaring the module, and pass dependencies to the module content through formal parameters:
Define (['. / averse,'. / b'], function (dep1, dep2) {a.doSomethimg () b.doSomething ()}); CMD specification
As opposed to the AMD specification, there is the CMD specification, the full name: CommonModule Definition.
Is a common module definition
This was put forward by a domestic jade uncle (who is also a big shot).
Module definition: define (factory)
CMD supports dynamic introduction:
Define (function (require, exports, module) {var a=require ('. / a') a.doSomethimg () var b=require ('. / b') b.doSomething ()})
When you need a dependent module, you can call require () to introduce it at any time.
CMD favors reliance on proximity; AMD favors dependence prefix
CMD is deferred execution; AMD is early execution.
CMD performs well because it is executed only when the user needs it; AMD has a good user experience because there is no delay and the dependent module executes ahead of time
The biggest difference between AMD and CMD is that the timing of execution of dependent modules is different.
Compatible with multiple module specifications
Create a hello method that allows the hello method to run in different environments and is compatible with Node, AMD, CMD and common browsers
It's a good habit to precede an anonymous function. Name is the method name and definition is the method body.
Check whether the environment is AMD or CMD or Node by typeof
The result of module execution can be mounted in the window variable, so that you can call the
At this point, the study on "how to understand the package and NPM in Node.js" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 260
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.