In-depth understanding of the NPM package

Package with NPM

Specification defines the CommonJS packet is actually very simple, it is described by the package file structure and the two parts, the former files for various tissue package, the latter is used to describe information packet for external read taking analysis.

Packet structure

Package is actually an archive file, a directory that is directly packaged as .zip or tar.gz file format, decompression after the installation is reduced to the directory. CommonJS package directory full compliance with specifications should contain the following files.

  1. package.json: package description file.
  2. bin: directory for the executable binary files.
  3. lib: directory of JavaScript code for storing.
  4. doc: the directory used to store documents.
  5. test: means for storing the test code.

You can see, CommonJS package specification in terms of documentation, testing, etc. are done considering. When made public after a packet is completed, the user can see unit testing and documentation, it gives people a sense of practical and reliable.

Package description file

Package description files for the expression of non-code information relating to it is a JSON format file --package.json, located under the root directory of the package, it is an important part of the package. And all acts are closely related field npm package description files.

CommonJS must define the field package.json file.

  1. name: the package name. It requires specification defines the composition of lowercase characters and numbers, may contain \ _ and -, but spaces are not allowed. Package name must be unique in order to avoid misunderstanding of the same name conflict when announced. In addition, NPM also recommended not incidental to the node js or repeat indicate that it is a JavaScript or Node module in the package name.
  2. description: Introduction package.
  3. version: version number.
  4. keywords: Keywords array, NPM is mainly used for classification search. A good keyword array help users quickly find the package you write.
  5. maintainers: package maintainer list, each of the maintainer by the name, email and web three attributes.

NPM carried out by the certification authority property.

  1. contribuctors: a list of contributors.
  2. bugs: a can of bug feedback page address or email address.
  3. licenses: a list of the current license used by the package, indicating that the package uses in which a license.
  4. repositories: hosting the source code for a list of locations to indicate which way and address the source code can access the package through.
  5. dependencies: List Packet using current package depends. This property is very important, NPM will automatically load the package using this property.

In addition to mandatory field, the specification also defines part of an optional field

  1. homepage: website address of the current package.

  2. os: a list of operating systems supported. The value of these operating systems include aix, freebsd, Linux, macos, solaris, vxworks, windows. If the setting list is empty, no operating system to make any assumptions.

  3. CPU: Support List CPU architecture.

  4. engine: Supported js engine list, a list of valid values ​​include engine ejs, flusspferd, gpsee, jsc, spidermonkey, narwhal, node, v8.

  5. builtin: flag whether the current packet is built into the standard component in the underlying system.

  6. directories: Package catalog description.

  7. implements: achievement of a standardized list, mark the current package that implements the specification CommonJS.

  8. scripts: Script description of the object. It is mainly used to install the package manager, compiling, testing and unloading packages, providing a hook mechanism.

    //示例如下:
    "scripts":{
    	"install":"install.js",
    	"uninstall":"uninstall.js",
    	"build":"build.js",
    	"test":"test.js"
    }
    

Define package specification can help solve the problem Node dependencies installed, while NPM is based on the specification implementation. Initially, NPM tools are there Issac Z.Schlueter create a separate, provided to the Node Package Manager node services, need to be installed separately. Later, when integrated into the Node v6.3 version as the default package manager, as part of the installation package together.

Described in the specification file in the package, NPM actual field needs mainly name, version, description, keywords, repositories, author, bin, main, scripts, engines, dependencies, devDependencies.

The difference between the packet specification is more author, bin, main and devDependencies four fields.

author: Package authors.

bin: Some package authors hope that the package can be used as a command line tool. After configuring the bin field, by npm install package_name -gcan be added to the script command execution path, then you can execute directly on the command line. -G command module by mounting the package called global model.

main: module introduction method require () when introduced into the package, checks the priority field, and as the remaining modules in the package inlet. If the field does not exist, require () method looks index.js in the package directory, index.node, index.json file as the default entry.

devDependencies: Some modules only need to rely on in the development. This configuration attribute, the developer may be prompted subsequent installation package dependencies.

//以下是node比较有名的框架express项目的package.json文件
{
  "name": "express",
  "description": "Fast, unopinionated, minimalist web framework",
  "version": "4.17.1",
  "author": "TJ Holowaychuk <[email protected]>",
  "contributors": [
    "Aaron Heckmann <[email protected]>",
    "Ciaran Jessup <[email protected]>",
    "Douglas Christopher Wilson <[email protected]>",
    "Guillermo Rauch <[email protected]>",
    "Jonathan Ong <[email protected]>",
    "Roman Shtylman <[email protected]>",
    "Young Jae Sim <[email protected]>"
  ],
  "license": "MIT",
  "repository": "expressjs/express",
  "homepage": "http://expressjs.com/",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "dependencies": {
    "accepts": "~1.3.7",
    "array-flatten": "1.1.1",
    "body-parser": "1.19.0",
    "content-disposition": "0.5.3",
    "content-type": "~1.0.4",
    "cookie": "0.4.0",
    "cookie-signature": "1.0.6",
    "debug": "2.6.9",
    "depd": "~1.1.2",
    "encodeurl": "~1.0.2",
    "escape-html": "~1.0.3",
    "etag": "~1.8.1",
    "finalhandler": "~1.1.2",
    "fresh": "0.5.2",
    "merge-descriptors": "1.0.1",
    "methods": "~1.1.2",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.3",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~2.0.5",
    "qs": "6.7.0",
    "range-parser": "~1.2.1",
    "safe-buffer": "5.1.2",
    "send": "0.17.1",
    "serve-static": "1.14.1",
    "setprototypeof": "1.1.1",
    "statuses": "~1.5.0",
    "type-is": "~1.6.18",
    "utils-merge": "1.0.1",
    "vary": "~1.1.2"
  },
  "devDependencies": {
    "after": "0.8.2",
    "connect-redis": "3.4.1",
    "cookie-parser": "~1.4.4",
    "cookie-session": "1.3.3",
    "ejs": "2.6.1",
    "eslint": "2.13.1",
    "express-session": "1.16.1",
    "hbs": "4.0.4",
    "istanbul": "0.4.5",
    "marked": "0.6.2",
    "method-override": "3.0.0",
    "mocha": "5.2.0",
    "morgan": "1.9.1",
    "multiparty": "4.2.1",
    "pbkdf2-password": "1.2.1",
    "should": "13.2.3",
    "supertest": "3.3.0",
    "vhost": "~3.0.2"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "scripts": {
    "lint": "eslint .",
    "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
    "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
    "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
  },
  "__npminstall_done": "Sun Feb 16 2020 11:22:54 GMT+0800 (GMT+08:00)",
  "_from": "[email protected]",
  "_resolved": "https://registry.npm.taobao.org/express/download/express-4.17.1.tgz"
}

NPM common functions

Package Specification CommonJS theory, NPM is a practice wherein a. For Node terms, NPM help complete the release, installation and rely on third-party modules and so on. By NPM, a well is formed between the Node and the third-party ecosystem module.

Installation dependencies

Installation dependencies are the most common usage of NPM, its execution statement is npm install express .. After executing the command, NPM is created in the current directory node_module directory, then create a directory under node_module express directory, then uncompress it to this directory.

After installation dependencies, directly call the code require ( 'express'), can be introduced into the bag, the require () method of analysis will be done through the path, find a path to the location of the module where the express. Introduction and installation module packages are complementary.

Global Mode Installation

If contains a command-line tool, you need to execute npm install express -gcommands globally installed. It should be noted that the overall pattern will not install a module package as meaning a global package, does not mean he can be referenced from anywhere via require () to it.

Global Mode This title does not really accurate, there are many misleading. Indeed, -g a package is installed as executable commands globally available. He described the package according to the bin field of the configuration file, a link to the actual script executable under the same Node path:

"bin":{
	"express":"./bin/express"
}

In fact, all the modules have been installed global mode installation into a unified directory, the directory may be out of operation by:

path.resolve(process.execPath, ‘…’ , ‘lib’ , ‘node_modules’);

If the position of the Node executable file is / user / local / bin / node, then the module directory is / user / local / lib / node_modules. Finally, by way of the soft link bin field configuration executable file links to the next executable directory of Node.

Installation from a local

For some packages not released to the NPM, or because the network reasons can not be directly installed package, the package can be downloaded to the local, and the local installation. Package.json local installation only need to specify the file where to position NPM can: It can be an archive file that contains package.json, it can be a URL address, it can also have package.json file directory location of the next directory . Specific parameters are as follows:

npm install <tarbar file>
npm install <tarbar url>
npm install <folder>
Installing from unofficial sources

If you can not install the official sources, can be installed by mirroring. In executing commands, add --registry=http://registry.urlto, examples:

npm insatll underscore --registry=http://registry.yrl

If the installation process is almost a mirror source used, an executable command specifies the default source

npm config set registry http://registry.url

NPM potential problems

As a tool for the module and package services, NPM is very convenient. It is essentially already a shared platform package, everyone can contribute and package them to share this platform, you can use them for free under license permit

Published 27 original articles · won praise 5 · Views 6110

Guess you like

Origin blog.csdn.net/systempause/article/details/104358449