How to Use npm to publish the Node. JS package

Source: Internet
Author: User

How to Use npm to publish the Node. JS package

This article describes how to use npm to publish a Node. JS package. This article describes how to configure package. json, register an npm account, and publish a package. For more information, see

 

 

Npm is the package manager of Node. JS. During Node. JS development, it is often used to install/uninstall the package. In fact, the work of releasing a package is also done by it.

Configure package. json

To package a program, you must configure various settings, which are specified by package. json in the root directory of the program package. The content of package. json must be in strict JSON format, that is:

1. The string must be enclosed in double quotation marks rather than single quotation marks;
2. Attribute names must be enclosed in double quotation marks;
3. Do not add a comma after the last attribute.

There are many attributes of the configuration object. For details, refer to the following common items:

1. name: the package name. It cannot be the same as the existing package.
2. version: version number.
3. description: a brief introduction.
4. author: author information. Includes three attributes: name, email, and url.
5. bin: if the program contains executable files (mainly called in the command line), you can specify multiple files here.
6. main: The program entry for calling this package using require.
7. dependencies: the dependent package. You can specify the version number.
After package. json is configured, You can package and install package locally to test whether the program works properly. The installation command is:

The Code is as follows:


Npm install <local path>


In addition, you should note that if you want the executable programs in the package to run in the Node. JS environment, add the following line at the beginning of the program entry file:

The Code is as follows:


#! /Usr/bin/env node


Without this line, it will be opened by default, instead of running in the Node. JS environment.

 

Register an npm account

To publish the package to npm, you must register an account first. Npm does not provide a webpage registration wizard. The registration should also be done through the command line:

The Code is as follows:


Npm adduser


After the command is executed, a prompt is displayed, indicating the user name, Email, and password. After the command is entered, wait for a while.

 

Release package

After the preparation is complete, you can run the following command to release the package:

The Code is as follows:


Npm publish <local path>


To update the package, you only need to modify the version number in package. json and re-execute the release command.

Related Article

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.