NPM Guide (ii)

Source: Internet
Author: User

Locally installed NPM package (installing NPM packages locally)

There are two ways to install NPM packages: Global, local (this project). The type of installation you choose depends primarily on how you want to use the installation package.

If you want to rely on these packages in the modules you write and want to use node. js require, this is used to install these packages locally. This is the default behavior of a NPM install. On the other hand, if you want to use his command line tool. Just like the grunt CLI . Then you will choose to use the global installation form.

To learn more about the install command line use, click CLI Doc page

Installation (installing)

A package can be downloaded using the following command

NPM Install < Package_name >

This command will create a node_modules directory in your current directory (when this directory does not exist) and include the package you downloaded.

In order to verify that the installation is correct, you can check the nodemodules folder to see if you have the package you installed. You can also view them by running commands. Run the LS nodemodules on a UNIX systemand run dir node_moduleson the Windows system.

Take this package as an example of installing Lodash . Make sure the installation is complete correctly, and see if there is a loadsh folder in the Node_modules directory

> npm install lodash> ls node_modules for                 Windows #= Lodash

# # # that version of the package was installed? (Which version of the package is installed?)
If there is no **package.json** file in the directory of your project, then we are installing the latest package. If there is **package.json** this file. The latest version needs to meet the rules for the use of the package declared in Package.js (that is, whether this version is compatible with the current project).

# # # Use installed packages (using the installed package)
Once the package is installed in **node_modules**, you can use the package in your code. For example, if you create a module, you can use **require** to refer to him.
For example: Create a index.js file and write the following code in the file:

var Lodash =  require (' Lodash 'var output = Lodash.without ([1, 2, 3], 1);   Console.log (output);

Run code:node index.js. output: [2,3]

If you do not install this package, you will get an error:

module.js:340    throw  err;           ^' Lodash '

NPM Guide (ii)

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.