Detailed Node.js package of engineering directory and the use of NPM Package Manager _node.js

Source: Internet
Author: User
Tags echo command install node npm install node

Engineering Catalogue

Having learned the above knowledge, we can now complete the planning of a project directory. As an example of writing a command-line program, we typically provide both command-line and API patterns, and we use a three-party package to write code. In addition to the code, a complete program should have its own documentation and test cases. Therefore, a standard engineering directory looks like this below.

-/home/user/workspace/node-echo/  # Engineering Directory
  -bin/             # Store command line related code
    Node-echo
  + doc/             # Store document
  -lib /             # Store API-related code
    echo.js
  -node_modules/         # Store three-party packages
    + argv/
  + tests/            # store
  test Cases Package.json          # meta data file
  readme.md            # description file

Some of the documents read as follows:

/* Bin/node-echo
/var argv = require (' argv '),
  echo = require ('. /lib/echo ');
Console.log (Echo (Argv.join));

/* Lib/echo.js */
module.exports = function (message) {return message
  ;
};

/* Package.json * * "
  name": "Node-echo",
  "main": "./lib/echo.js"
}

In the above examples, different types of files are stored and the modules are loaded directly using the three-party package name via the Node_moudles directory. In addition, after defining the Package.json, the Node-echo directory can also be used as a package.

Npm

NPM is a package management tool that is installed with Nodejs to solve many of the problems with NODEJS code deployments, and common usage scenarios include the following:

    • Allows users to download a Third-party package from the NPM server for local use.
    • Allows users to download and install other written command-line programs from the NPM server for local use.
    • Allow users to upload their own packages or command-line programs to the NPM server for others to use.

As you can see, NPM has built a nodejs biosphere, where Nodejs developers and users can exchange the same. The following describes how to use NPM in each of the three scenarios.

Download the three-party package
when you need to use a three-party package, you first need to know which packages are available. Although npmjs.org provides a search box can be based on the package name to search, but if you want to use the name of the three-party package is not sure, please Baidu. After you know the package name, such as the argv in the example above, you can open the terminal in the engineering directory and use the following command to download the three-party package.

$ npm Install argv ...
argv@0.0.2 NODE_MODULES\ARGV

Once downloaded, the argv package is placed in the Node_modules directory under the engineering directory, so it is only necessary to pass require (' argv ') in the code without specifying a three-way package path.

The above command defaults to download the latest version of the three-party package, if you want to download the specified version, you can add @<version> behind the package name, such as the following command to download the 0.0.1 version of the argv.

$ npm Install argv@0.0.1 ...
argv@0.0.1 NODE_MODULES\ARGV

If the use of the three-party package is more, in the end of the next package to install a command is too human. NPM has therefore extended the Package.json field to allow for the declaration of a three-party package dependency. Therefore, the Package.json in the top example can be rewritten as follows:

{
  ' name ': ' Node-echo ', '
  main ': './lib/echo.js ',
  ' dependencies ': {
    ' argv ': ' 0.0.2 '
  }
}

After this process, the NPM Install command can be used to bulk install the three-party package in the engineering directory. More importantly, when the Node-echo is uploaded to the NPM server, when someone downloads the package, NPM relies on the three-party package as stated in the package to automatically download further-dependent tripartite packages. For example, when using the NPM install Node-echo command, NPM automatically creates the following directory structure.

-project/-
  node_modules/
    -node-echo/
      -node_modules/
        + argv/
      ...
  ...

As a result, users need only care about the three-party packages they use directly, without having to solve all the dependencies of their packages themselves.

To install a command line program
downloading the installation of a command-line program from the NPM service is similar to a three-way package. For example, the Node-echo in the previous example provides command-line usage, and as long as Node-echo has configured the relevant Package.json fields for the user, you only need to install the program with the following command.

$ NPM Install Node-echo-g

The-G in the parameter indicates a global installation, so Node-echo is installed by default to the following location, and NPM automatically creates the soft chain files that are required under the Linux system or the. cmd files that are required under the Windows system.

-/usr/local/        # Linux system
  -lib/node_modules/
    + node-echo/
    ...
  -bin/
    Node-echo ...
  ...

-%appdata%\npm\      # Windows System
  -node_modules\
    + node-echo\
    ...
  Node-echo.cmd ...
  

Publishing code
you need to register an account before you use NPM to publish your code for the first time. Run NPM AddUser under the terminal, then follow the prompts. After the account is done, we need to edit the Package.json file and add the required fields to NPM. Then the above Node-echo example, the necessary fields in Package.json are as follows.

{
  ' name ': ' Node-echo ',      # package name, required to maintain unique
  ' version ' on NPM server: ' 1.0.0 ',      # Current version number
  ' dependencies ': {       # Three-party package dependencies, you need to specify the package name and version number
    "argv": "0.0.2"
   },
  "main": "./lib/echo.js",    # Portal Module Location
  "Bin": {
    " Node-echo ":"./bin/node-echo "   # command line program name and Main module location
  }
}

After that, we can run NPM publish release code in the directory where Package.json is located.

Version number
The version number is exposed when you download and publish your code using NPM. NPM uses the semantic version number to manage the code, which is briefly described here.

The semantic version number is divided into x.y.z three digits, representing the major, minor, and patch version numbers, respectively. When the code changes, the version number is updated according to the following guidelines.

+ If you just fix the bug, you need to update the z-bit.

+ If the feature is new, but backward compatible, you need to update y bit.

+ If there is a big change, down is not compatible, you need to update the x bit.
With this assurance in the version number, when declaring a three-party package dependency, you can rely on a range of version numbers in addition to a fixed version number. For example, "argv": "0.0.x" means the latest version of argv that relies on the 0.0.x series. All version number ranges that NPM supports specify the way to view official documents.

A little bit of a Turing
In addition to the sections described in this chapter, NPM provides a number of features, and there are many other useful fields in Package.json. In addition to viewing official documents in npmjs.org/doc/, here are some of the NPM common commands.

NPM provides a number of commands, such as install and publish, that use NPM help to view all commands.

    The
    • uses NPM help to view the details of a command, such as NPM assistance install.
    • use NPM install. g in the directory where the Package.json resides to install the current command-line program locally, which can be used for local testing before publishing. The
    • uses NPM update <package> to update the corresponding module inside the Node_modules subdirectory in the current directory to the latest version.
    • Use NPM update <package>-G to update the corresponding command-line program for the global installation to the latest version. The
    • uses NPM cache clear to empty the NPM local cache for people who publish new versions of code with the same version number. The
    • uses NPM unpublish <package>@<version> to undo a release of a version code that you have published.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.