node. JS Learning 4

Source: Internet
Author: User

Node. JS Package Manager

NPM is the official package management tool from node. js, and has become the standard publishing platform for node. JS packages for the release, propagation, and dependency control of node. JS packages.

NPM is to node. js as Pip is to Python,gem for Ruby,pear to PHP.

NPM by default, the package is searched or downloaded from http://npmjs.org, and the package is installed into the node_modules subdirectory of the current directory.

NPM differs from PIP or gem: The Gem or PIP is always installed in global mode, making the package available to all programs, with the advantage that it can improve the reuse of the program and avoid multiple copies of the same content, but the disadvantage is that it is difficult to handle different versions of dependencies,

NPM installs the package to the current directory by default, or locally, with the advantage that there will be no conflicting programs that rely on different versions of the package, while mitigating the API compatibility pressure of the package author, but the flaw is that the same package may be installed many times.

Local mode: Can be used through require, do not register path.

Global mode: cannot be used through require, you need to register path.

In summary: When a package needs to be part of the project runtime, it is obtained through local mode and is installed using global mode if it is to be used at the command line. (Plus-G is the global mode when NPM is installed)

NPM provides an interesting command of NPM link, which features a symbolic connection between a local package and a global package.

With the NPM link command, you can use the global mode to install the package through require! ( but NPM link does not support Windows)

The NPM link command can also link a local package to the global, in addition to linking the global package to local. The method used is to run the NPM link command in the package directory (the directory where Package.json is located).

If we were to develop a package, it would be very easy to test between different projects with this approach.

To publish a package:

Use NPM init to produce a standard-compliant Package.json.

(1) Under the specified folder: NPM init

Then enter the required name in turn, and a standard Package.json file is generated under the folder.

(2) Create a index.js as the interface of the package, a simple package is finished.

Before the release, you also need to obtain an account for future maintenance of their own packages, using NPM AddUser prompted to enter the user name, password, mailbox, waiting for account creation to complete.

When you're done, you can use the NPM WhoAmI quiz to see if you've got your account.

Next, run NPM publish in the directory where the Package.json is located, and wait a few moments to complete the release.

Open your browser and you'll find the package you just released in http://search.npmjs.org/.

You can then install it using the NPM install package name command on any computer in the world. ( Oh, wow!) It's so cool! )

If the package is updated in the future, simply modify the version field in the Package.json file and then re-use the NPM Publish command.

If you are dissatisfied with a published package, you can use NPM unpublish naming to Unpublish.

node. JS Learning 4

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.