Nodejs in NPM

Source: Internet
Author: User
Tags node server

NPM is a module-dependent management tool for node. js. As a tool used by developers, the main solution is to solve problems when developing node. js. As RubyGems the importance of Ruby developers and Maven for Java developers, NPM is self-evident to developers and communities of node. js. This article includes five points: Package.json, NPM configuration, npm Install command, NPM Link command, and other NPM commands.

Package.json

The NPM command runtime reads the current directory's Package.json file and interprets the file, which is based on the packages/1.1 specification. In this file you can define your app name (name), app description (description), keyword (keywords), version number, app configuration item (config), home page (homepage), author (author), The Resource warehouse address (repository), the address of the bug (bugs), the authorization method (licenses), the directory (directories), the application portal file (main), the command-line file (bin), the application-dependent module (dependencies ), Development environment dependent modules (Devdependencies), run engines (engines), and scripts (scripts).

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

{    "name": "Test",    "version": "0.1.0",    "description": "A Testing package",    "author": "A messed author <[email protected]> ",    " 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) to index, and when other applications reference our module require (' Test '), the value of this main index.js file is called. The script (scripts) uses a hash table to define several different commands. The defined node Server.js in Script.start will be called when NPM start is called, and the command defined in the corresponding scripts.test in the same NPM test call is invoked. When some native modules need to be compiled, we can define pre-compiled and compiled commands. In this example, the application dependency module (dependencies) and the Development Environment Dependent module (devdependencies) are also defined. Application dependent modules are installed at install time into the Node_modules directory of the current module. The development environment relies on modules that are primarily used in the development environment, using the command install or link plus parameter-dev of the command NPM to install into the Node_modules directory of the current module.

We also notice that the version number in Package.json is >= 0.6.7 some are 1.x.x, what is the difference? NPM uses semantic version recognition for version management. Not all modules provide backward compatibility, and sometimes some modules are not backwards compatible 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:

0.4.2

    • Major Version (0)
    • Sub-version (4)
    • Patch version (2)

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

the configuration of NPM

NPM has many default configurations. You can either use these default configurations, or you can modify these default configurations, or even modify them under environment variables or command lines. The weights of the configuration are defined in the following order:

    1. The command line, which uses the-prefix parameter. For example-foo bar, the value of the Set Variable foo is "bar". The value of Foo is set to True after-foo without a value parameter.
    2. Environment variables, all environment variables prefixed by Npm_config_. For example Npm_config_foo = bar, set variable foo to "bar".
    3. User-defined. All variables are stored in the $HOME/.NPMRC file.
    4. Global. All $PREFIX variables in the/ETC/NPMRC file. $PREFIX variables can be obtained via NPM prefix-g, which is typically/usr/local by default.
    5. Built-in configuration. Run the./configure variable as defined by the installation. Available by command Curl http://npmjs.org/install.sh | Env Npm_config_foo=bar SH settings.

The use of configuration can give us a lot of flexibility. For example, when we use NPM Install, the default repository address https://registry.npmjs.org/ is not very satisfied, we can use the following command to change the repository address.

Or is not satisfied with NPM default VI editor, direct command NPM set editor mate. The configuration of NPM can be obtained by command NPM config ls. This command is to get the modified configuration, to get all configurations including the default configuration plus the-l parameter. It is worth noting that the developer modifies the value of the Registry property by means of the NPM Config set registry "an other registry", and it is important to understand the negative effect of this value modification. Once the registry value is set, when you want to publish a module, the module is published in the modified repository instead of the original default repository. The other repository is a replica of the original default repository, timed to fetch resources from the default repository. In general, there is no ability to synchronize the modules of their new home to the default repository. This will cause your module to be found in the modified repository and not found in other repositories.

npm Install command

Installing the module only requires the NPM install Express Connect command to bring us a lot of convenience. The path of the installation module is divided into two types:

    • The global path, which is the installation mode with the parameter-G. This command installs the module under $PREFIX/lib/node_modules, and can be used to view the installation directory of the global module via the command NPM root-g. The bin defined in Package.json will be installed in the $PREFIX/bin directory if the module has a man page installed in the $PREFIX/share/man directory.
    • Local path, without the-g parameter. From the current directory to find the root directory/There is no Node_modules directory, a module installed in the directory under the Node_modules directory, if not found the module installed into the current directory Node_modules directory. The bin defined by PACKAGE.JOSN will be installed in the Node_modules/.bin directory, and the man page will not be installed.

What type of installation do we need to choose? The global mode allows you to avoid the need to find a module, and if you do not want to try to prevent the global mode.

    • If we are just require (' pkg ') a module, we do not need to use global mode.
    • If we need to call on the command line, we need to use the global schema. Because this installation installs the definition under the bin in the Package.josn to the $PATH directory.

Some modules we need to call in the command line and want to require (' pkg '), such as Coffee-script . Then we can use the global mode to install, and then use the next section of the command to tell NPM link to link it to the local node_modules directory.

Don't worry that the commands defined in the script in Package.josn will not run because they are not installed globally. For example, the vows of devdependencies defined in the examples. When you call NPM test, NPM places the Node_modules/.bin directory at the front of the environment variable $PATH.

NPM Link Command

For developers, this is the most valuable command. Suppose we develop a module called test, and then we refer to the module in Test-example, and each time the test module changes we need to be reflected in the Test-example module. Don't worry, with NPM link command everything is very easy.

First we need to link the test to the global schema:

CD ~/work/node/test # Enter Test module catalog NPM link # Create link to $prefix/lib/node_modules

Then the module of test will be linked to the $PREFIX/lib/node_modules, just like on my machine $PREFIX point to/usr/local, then/usr/local/lib/node_modules/test will be linked to ~/wo Under the Rk/node/test. The execution script bin/test.js is linked to the/usr/local/bin/test.

Next we need to refer to the test in the Test-example project:

CD ~/work/node/test-example # Enter the test-example module directory NPM link Test # Link the global mode module to the local

The NPM Link Test command will go to the $PREFIX/lib/node_modules directory to find the module named Test, locate the module and link $PREFIX/lib/node_modules/test directory to ~/work/node/ Test-example/node_modules/test this directory.

Now any changes on the test module will be mapped directly to test-example. For example, suppose we develop many applications, each using Coffee-script:

NPM Install COFFEE-SCRIPT-G # Global mode installs COFFEE-SCRIPTCD ~/work/node/test # into the development directory NPM Link Coffee-script # put the global mode Coffee-scrip The T module links to the local node_modules under CD. /test-example # Enter another development directory NPM link Coffee-script # link the global mode Coffee-script module to the local NPM update coffee-script-g # update global mode coffee -script, all link past projects have been updated at the same time.

As you can see, NPM link is useful when developing a module that is referenced by multiple modules. Windows users will think, I do not have a UNIX link tool under what to do? Don't worry. This feature is available as long as your node. JS supports Fs.symlink.

other NPM commands

There are many useful commands in the NPM command. NPM Explore. --Git pull Origin master to update the current git repository. NPM Edit. , edit all dependent modules of the current module. NPM Docs coffee-script, open the documentation for the Coffee-script module. NPM outdated coffee-script to see if there is a new version of Coffee-script. NPM submodule. , you can ask that your dependent modules be installed from the GIT repository instead of from registry. Because the author's git repository is always up-to-date, registry is a stable version of the module author's release. You can even use NPM to program.

var npm = require (' NPM '); Npm.load ({}, function (err) {    if (err) return commandfailed (err);    Npm.on ("Log", function (message) {        if (ARG) console.log (message)    })    var requirements = Json.parse ( Fs.readfilesync (' Config/requirements.json '));    Npm.commands.install (Requirements, function (err, data) {        if (err) return commandfailed (ERR);    });

As a developer tool for node. JS, NPM has been thinking about many scenarios for us. This is also the node. JS community that consistently recommends it for developer module dependency management tools.

Nodejs in NPM

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.