Young man, Your Nodejs (3)

Source: Internet
Author: User

Today, our topic composition is Nodejs, and I try to describe it as similar, but it's not the same. It is a world in which the beauty of a unique body is more attractive. Perhaps just do not understand, perhaps time will give the answer, Nodejs you deserve to have.

In 2 we touched on a little bit about the Nodejs package, so take a hot iron and talk about the package management tool NPM in this article.

1.NPM Package Management Tools

The node. JS Package Manager, NPM, is the official node. JS Package Manager tool, and he has become the standard publishing platform for node. JS packages for the release, propagation, and dependency control of node. JS packages. NPM provides command-line tools that allow you to easily download, install, upgrade, remove packages, and also let you develop The provider publishes and maintains the package.

This thing will be installed automatically with the installation of Nodejs, and by entering npm-v on the command line to see the version of NPM installed, you can know indirectly whether NPM has been successfully installed.

E:\nodejs>npm-v
2.14.12

Here's a quick look at the role of NPM:
Allows users to download third-party packages written by others from the NPM server for local use.
Allows users to download and install command-line programs written by others from the NPM server for local use.
Allows users to upload their own packages or command-line programs to the NPM server for others to use.

2. Download the installation package using NPM

NPM Install PackageName or NPM i PackageName

The successful installation of the package will be placed in the Node_modules subdirectory of the current directory, and NPM will automatically resolve its dependencies while acquiring the package, and will download its dependent packages.

2.1 Local mode and global mode

When using NPM to install packages, there are two modes, a local mode (without parameter-G), a global mode.
Local Installation without-G
1. Place the installation package under./node_modules (the directory where the NPM command is running), and if there is no Node_modules directory, the Node_modules directory is generated under the directory where the NPM command is currently executing.
2. A locally installed package can be introduced via require ().

By default, the install command without the parameter-G is in local mode, and the package will be installed under subdirectory node_modules. node. JS's require tries to search for the directory when it loads the module, so packages installed using local mode can be referenced directly.


Global Installation plus-G
1. Place the installation package under/usr/local.
2. Can be used directly on the command line.
3. A locally installed package cannot be introduced via require ()

With the-G installation, NPM installs the package to the system directory, such as/usr/local/lib/node_modules/, and the files contained in the Bin field in the Package.json file are linked to/usr/local/bin/. /usr/local/bin/is defined by default in the PATH environment variable, so you can run the Supervisor script.js command directly on the command line as you did before the Supervisor tool.


Why use global mode? Most of the time it is not because many programs have the potential to use it, but rather because the global schema is used to reduce multiple replicas, but because local mode does not register the PATH environment variable.
Required Register Path
Local mode YES NO
Global Mode NO YES
All in all, when we want to take a package as part of the project runtime, get it through local mode, and if you want to use it at the command line, use global mode to install it.

3. Using NPM to publish a package
3.1 Making a Package.json file that complies with the COMMONJS specification
By using NPM init, you can generate a standard Package.json based on interactive questions and answers.
==>>> such as creating a directory of MyPackage, and then running NPM init in this directory

E:\NODEJS\MYPACKAGE>NPM Init
This utility would walk you through creating a Package.json file.
It is covers the most common items, and tries to guess sensible defaults.

See "NPM help JSON" for definitive documentation in these fields
And exactly what they do.

Use ' npm install <pkg>--save ' afterwards -to-install a package and
save it as a dependency in the Package.json file.

Press ^c at no time to quit.
Name: (mypackage) YZR
Sorry, name can no longer contain capital letters.
Name: (mypackage) Yzr
version: (1.0.0) 0.0.1
description:test
entry point: (index.js) Default.js
Test command:make Test
git repository:http://github/yangzhiran/mypackage.git
Keywords:nodejs Yzr
Author:yzr
License: (ISC) MIT
About-to- write to E:\NodeJS\myPackage\package.json:

{
"name": "Yzr",
"version": "0.0.1",
" description": "Test",
" main": "Default.js",
"Directories": {
"Doc": "Doc",
"test": "Test "
  },
"Scripts": {
"test": "Make Test "
  },
"Repository": {
"type": "Git",
"url": "Http://github/YangZhiRan/mypackage.git"
  },
"keywords": [
"Nodejs",
"Yzr"
  ],
" Author": "YZR",
" License": "MIT"
}


Is this OK? (yes) Yes

e:\nodejs\mypackage>


3.2 Get a NPM account
before releasing, we need an account to be used to maintain our own package in the future, using NPM AddUser 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.

E:\NODEJS\MYPACKAGE>NPM AddUser
Username:yangzhiran
NPM WARN Username must be lowercase
Username:yangzhiran
Password:
Email: (This was public) [email protected]


E:\NODEJS\MYPACKAGE>NPM WhoAmI
Yangzhiran


3.3 Release
Run NPM publish in the same directory as the Package.json file
===> visit https://www.npmjs.com/to find the package you've released.


E:\NODEJS\MYPACKAGE>NPM Publish
+ [email protected]


3.4 Update Version
You only need to modify the version field in the Package.json file and then reuse the NPM Publish command.
3.5 Uninstalling the Package
Use the NPM unpublish command to cancel the publication.

Here are the settings for the NPM agent, which you may want to look at:

Set HTTP proxy: NPM config set proxy http:ip address: port

Cancel proxy settings: NPM config Delete proxy

Next:nodejs Commissioning

Young man, Your Nodejs (3)

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.