Node.js's NPM Package Manager Basics Tutorial _node.js

Source: Internet
Author: User
Tags redis node server

Configuration

NPM set

npm set Init-author-name ' Your name '
npm set init-author-email ' Your email '
npm set Init-author-url ' H Ttp://yourdomain.com '
npm set init-license ' MIT '

The above command is equivalent to setting the default value for NPM init, and the author name, message, home page, and license field for Package.json will be automatically written to the preset value when the NPM init is executed later. This information is stored in the ~/.NPMRC file in the user's home directory, allowing users to enter them without each item.
If a project has different settings, you can run NPM config for that project.
1.

NPM Set Save-exact True

When the above command is set to join a module, Package.json will record the exact version of the module, rather than an optional version range.

2.

npm config get prefix

3.

NPM Config set prefix/usr/local

NPM uses
Installation:

NPM Install GRUNT-CLI

Before installing, NPM install first checks to see if the specified module already exists in the Node_modules directory. If it exists, it is no longer reinstalled, even if the remote repository already has a new version.
If you want a module to be forced to reinstall, regardless of whether it's installed or not, you can use the-f or--force parameters.

Local Installation: Package will be downloaded to the current directory and can only be used under the current directory. After the installation is over, the current directory next to a node_modules directory, GRUNT-CLI installed in the inside.

NPM install-g GRUNT-CLI

Global Installation: Package will be downloaded to a specific system directory and the installed package can be used in all directories. Now it becomes/usr/local/lib/node_modules/grunt-cli,/usr/local/lib/node_modules/, the global installation directory.
1. Install the Devdependencies module configured in the current directory Package.json file

NPM Install

2. Install the local module file

NPM install./package.tgz

3. Install the module for the specified URL

NPM Install https://github.com/indexzero/forever/tarball/v0.5.6

4. Install the module contained in the directory specified in the local file system

NPM Install <folder>

5. Install and update the version configuration in Package.json

NPM install <name> [–save|–save-dev|–save-optional]

(1) The name of the module installed with the –save parameter and its version information will appear in the Package.json dependencies option
(2) The name of the module installed with the –save-dev parameter and its version information will appear in the Package.json devdependencies option
(3) The name of the module installed with the –save-optional parameter and its version information will appear in the Package.json optionaldependencies option
6. Install the specified version of the module

NPM Install <name>@<version>
Example:
npm Install underscore@1.5.2

7. The installation module specifies a version within the range of the version number

NPM Install <name>@<version range>

Example:

NPM install async@ ">=0.2.0 <0.2.9″

–force force pull remote resource, even if this module is already installed locally
Example:

NPM Install Underscore–force

8.-g or –global global installation module, if not, it will be installed in the Node_modules subdirectory of the current directory
Example:

NPM Install-g Express

If you want all modules to be forced to reinstall, delete the Node_modules directory and rerun the NPM install.

$ RM-RF node_modules
$ npm Install

Update

NPM Update [-G] [<name> [<name> ...]

Updates the module in the specified name list. The-g parameter updates a globally installed module.
If name is not specified and is not within a module, all packages that are updated by the current directory dependency are updated (both global and module), and if the current directory is in a module directory and the module depends on it, so do not specify name to run NPM update directly, It is best to run within a module to avoid updating to other modules that you do not want to update.

Unloading

NPM Uninstall Package

View
View the packages that were installed

NPM ls--depth=0

View specific Package specific information

NPM ls grunt-cli
npm info grunt-cli

Search

NPM Search Grunt-cli

Release
1.package.json
Package.json Description:
The NPM command runtime reads the Package.json file for the current directory and interprets the file, which is based on the packages/1.1 specification. In this file you can define your application name, application description (description), keywords (keywords), version number (versions), Applied configuration items (config), home page (homepage), author (author), Resource Warehouse address (repository), submit address for the bug (bugs), authorization method (licenses), directory (directories), application entry file (main), command line file (bin), Application dependent module (dependencies ), the Development Environment Dependency module (devdependencies), the Run engine (engines), and the script (scripts).

For developers, the development and release module relies on his correct understanding of the meaning contained in this document Package.json. Here we use a common example of this article to illustrate:

{
  "name": "Test", "
  Version": "0.1.0",
  "description": "A Testing package",
  "author": "A messed author <messed@example.com> ",
  " dependencies ": {
    " Express ":" 1.x.x ",
    " Ejs ":" 0.4.2 ",
    " Redis ":" >= 0.6.7 "
  },
  " Devdependencies ": {
    " vows ":" 0.5.x "
  },
  " main ":" Index ",
  " bin ": {
    " Test ":"./bin/test.js "
  },
  " scripts ": {
    " start ":" Node Server.js ",
    " test ":" Vows Test/*.js ",
    "Preinstall": "./configure",
    "Install": "Make && make Install"
  },
  "engines": {
    " Node ': ' 0.4.x '
  }
}

In this example we define the application's entry file (main) as index, and when other applications reference our module require (' Test '), the value of this main index.js file is invoked. The script (scripts) uses a hash table to define several different commands. The defined node Server.js in Script.start is invoked at NPM start, and the same commands defined in the corresponding scripts.test in the same NPM test call are invoked. When some native modules need to be compiled, we can define precompiled and compiled commands.

The application-dependent module (dependencies) and the Development Environment Dependency module (devdependencies) are also defined in this example. The application dependency module is installed at installation into the Node_modules directory of the current module. The development environment relies on the module, which is used primarily in the development environment, to install the command install or link plus parameter-dev to the current module's Node_modules directory.

Name:package's name (because he will become part of the URL, so the Non-url-safe letter will not pass, also does not allow the ".", "_"), preferably in [] (http://registry.npmjs.org/ To search for the name you've taken is already there.
Version:package version, when package changes, version should also follow along with the change, and you declare the version of the need to pass the Semver check (Semver can own Google)
Dependencies:package application-dependent modules, that is, other people want to use this package, at least what things need to install. The application dependency module is installed in the Node_modules directory of the current module.
Devdependencies:package Development Dependent module, when the plug-in release automatically delete the irrelevant code. Use a file to record the plug-ins that are installed or needed in the current project, that is, someone else is going to develop on this package, you can install the required plug-ins for the project in one click.

2. Version number
It is also noted that some of the version numbers in Package.json are >= 0.6.7 some are 1.x.x, what's the difference? NPM uses semantic version recognition for versioning. Not all modules provide backward compatibility, and sometimes some modules cause no backward compatibility for some reason. So we need to define rules to ensure that the modules are available in certain versions, and that the latest versions are guaranteed, because those versions always modify bugs or improve performance. Let's take a look at the version-defined fields:

Example: 0.4.2

+ Large version (0)
+ Small version (4)
+ Patch version (2)
When a software is released, the default is version 1.0.0. If you post a patch later, add the last digit, such as 1.0.1. If you add new features and do not affect the original functionality, increase the middle number (that is, the minor version number), such as 1.1.0; If the introduced changes break backward compatibility, add the first digit (i.e. the large version number), such as 2.0.0.

In the definition above Package.json we are sure that the module will work in all Nodejs 0.4 and above and 0.5 below. The dependent module Redis can be run on all versions that are greater than or equal to 0.6.7, and the dependent module Ejs can only be guaranteed to run in the 0.4.2 version, which relies on module express to ensure compatibility greater than or equal to 1.0.0 and less than 2.0.0.

Generated

NPM Init

Used to initialize the build of a new Package.json file. It will ask the user a series of questions, if you feel that you do not have to modify the default configuration, all the way to return.
If you use-f (for Force), Y (for Yes), skip the questioning phase and generate a new Package.json file directly.

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.