The problem of Javascript

JavaScript was originally designed to run on the client side to control the interaction of the page; As server-side applications unfold, the following problems begin to emerge: (1) JavaScript has no concept of modules. No native closed scope support and dependency management; (2) JavaScript has no standard library. True file IO, Socket, encryption and other API standards; (3) JavaScript has no standard interface. A unified interface for database access. (4) JavaScript has no package management system. Dependency files cannot be automatically loaded and installed.

Due to the lack of unified standards and specifications, the above problems limit the wider application of server-side JS.

CommonJS specification

ServerJS (www.commonjs.org/) is not a language, but for…

(1) Server-side applications (2) command line applications (3) desktop GUI applications (4) Mixed applications

Many implementations of the CommonJS specification, such as Node.js, Narwhal, Wakanda, etc., implement common apis defined by the CommonJS specification, as well as specifications for writing, using, and maintaining modules and packages.

CommonJS package specification

C/C++ function library or Java/.Net class library It encapsulates modules related to a particular function for release, update, dependency management, and version control.

According to the CommonJS package specification (wiki.commonjs.org/wiki/packag…

(1) In a package.json file in the top-level directory; (2) Keep binary files in bin directory; (3) Save the documentation in the lib directory; (4) Save the documentation in the doc directory; (5) Save the unit test file in the test directory.

The require and package

The require method is also part of the CommonJS specification. In addition to mapping file modules, it can also import packages, whose parameters are the name of the package;

require('my_package');
Copy the code

The CommonJS specification requires that imported packages be in the node_modules directory of the current directory or a parent directory of the current directory, or the directory specified by the NODE_PATH environment variable. The require function looks in order from near to far.

package.json

The CommonJS specification specifies a file used to describe package characteristics: package.json, which includes:

{
  "name": "mynode"./ / package name. The value consists of lowercase letters, digits, and underscores.
  "version": "1.0.0".// Version number;
  "main": "app.js".// Main file name;
  "dependencies": {// The dependent package;
    "iconv-lite": "^ 0.5.0"."express":"4.0"."mysql": ["2.11"."2.0"."1.0"]},"devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "authors": "", author information array. Each element can contain name, enmail, and Web attributes;"license": "ISC"."description": "Here's a case pack I created to introduce CommonJS.".// package description;
  "maintainers":[
    {
    	"name":"ZDM"."email":"[email protected]"}].// Package maintainer information array;
  "bugs":[
    {
    	"mail":"[email protected]"."web":"http://test.zdm.com"}].// Bug submit url, url or email address;
  "licenses":[
    {
    	"type":"Apache License v2"."url":"http://www.apache.org/licenses/apache2.html"}].// License array;
  "repositories":[
    {
    	"type":"git"."url":"https://github.com/zdm/unit.git"}].// Storage address array;
  "keywords": ["ZDM"."Demo"."lesson"].// Key word count;
  "engines":""// A runnable version of the engine
}
Copy the code

NPM

With the CommonJS specification, Node.js already provides dozens of core modules, but these modules are insufficient in the wider project application; So third-party organizations and individuals develop more extensions: www.npmjs.com/


NPM :Node Package Manager, a tool for downloading, installing, finding, updating, and deleting third-party Node.js packages — all of which comply with the CommonJS Package specification; At the same time, it can also perform functions such as packaging, publishing modules, and resolving inter-module dependencies, which has become the standard of Node.js package management.

The NPM package management tool has been integrated in the node. js installation file.

The local installation

Install the downloaded NPM package in the current project working path:

NPM install package nameCopy the code

1. The installation package will be saved in the directory specified by the NPM root command. 2. You can import locally installed packages via require ().


Global installation

Install the downloaded NPM package globally:

NPM install Package name -gCopy the code

1. The installation package will be saved in the directory specified by the NPM root -g command. 2, can be directly used in interactive mode, can not be used for script files.

maintenance

Lists installed packages:

npm ls
Copy the code

Update installed packages:

NPM update [package name]Copy the code

Direct NPM update is to update all installed packages. The NPM update package name is to update the specified package.

Uninstalling installed packages:

NPM uninstall the package nameCopy the code

Create a package

Use the NPM init command to generate a package.json file in the current directory.


{
  "name": "mytest"."version": "1.0.0"."description": "My Test pack"."main": "app.js"."scripts": {
    "test": "tets.js"
  },
  "keywords": [
    "Test"]."author": "zdm"."license": "ISC"
}
Copy the code

Use the NPM help json command to view all the configuration options available to the package.json file. (The command line automatically opens a document)

Publish a package

If you want to open a customized package, you can publish it on npmjs.org: (1) Register an account at https://www.npmjs.org (can be omitted); (2) Use the NPM adduser command to register a new account or log in to an existing account; (3) Enter the configured package directory and run the NPM publish command to publish the package. (4) Go to nPMjs.org and search for just released packages.