How to publish a node. js package using NPM

Source: Internet
Author: User

How to publish a node. js package using NPM Heero.luo posted 3 year ago, viewed 5,065 times

NPM is the package Manager for node. js. When you are developing node. js, you often use it to install/uninstall packages. In fact, the work of releasing a package is also done by it.

Configure Package.json

To package a program, you first need to set up the settings, which are specified by Package.json in the program package root directory. The content of the Package.json must be in a strict JSON format , which means:

    • strings are enclosed in double quotation marks , but not in single quotes;
    • attribute names must be double-quoted ;
    • Never add a comma after the last attribute.

There are many properties of the configuration object, see here for a list of commonly used items:

    • Name: Package name, cannot be duplicated with existing packages.
    • Version: Revision number.
    • Description: A brief introduction.
    • Author: author information. Contains the name, email, and URL three properties.
    • Bin: If the program has an executable file (mainly called inside the command line), it is specified here, you can specify more than one.
    • Main: The program entry when invoking this package with require.
    • Dependencies: Dependent packages, you can specify a version number.

After the Package.json is configured, it can be packaged locally and installed once, the test program is working properly, the installation command is:

npm install <本地路径>

In addition, there is a unspoken rule to note that if you want the executable program in the package to run in node. js, add this line to the front of the program's entry file:

#!/usr/bin/env node

Without this line, it will open as a system default, rather than running in the context of node. js.

Sign up for NPM account

To publish the package to NPM, you need to register an account first. NPM does not provide the web version of the Registration Wizard. Registration is also done through the command line:

npm adduser

After executing this command, you will be prompted to enter the user name, Email, password, and then wait for a while.

Publishing Packages

The preparation is ready, and you can publish the package by executing the following command:

npm publish <本地路径>

If you want to update the package, just modify the version number in Package.json and then re-execute the Publish command.

How to publish a node. js package using 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.