Use NPM to publish the Node.js package tutorial _node.js

Source: Internet
Author: User

NPM is the Node.js Package Manager. It is often used to install/uninstall packages when developing Node.js. In fact, the job of publishing a package is also done by it.

Configure Package.json

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

1. Strings should be enclosed in double quotes, but not in single quotes;
2. Attribute name must add double quotes;
3. Never add a comma after the last attribute.

Configuration objects have a lot of properties, you can see here, here is a list of commonly used items:

1.name: Package name, cannot repeat with existing package.
2.version: Version number.
3.description: A brief introduction.
4.author: Author information. Contains the name, email, url three property.
5.bin: If there is an executable file (mainly called in the command line) in the program, specify it here and you can specify more than one.
6.main: Use require to invoke the program entry when this package is invoked.
7.dependencies: The dependent package, you can specify the version number.
After the Package.json is configured, you can pack and install it locally once, and the test program works properly, and the installation commands are:

Copy Code code as follows:

NPM Install < local path >

In addition, there is a hidden rule to note that if you want the executable program in the package to run in a node.js environment, then add one line to the front of the program entry file:
Copy Code code as follows:

#!/usr/bin/env node

Without this line, it will open in the default mode of the system instead of running in a node.js environment.

Register NPM Account

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

Copy Code code as follows:

NPM AddUser

After you execute this command, you will be prompted to enter your username, Email, password, and then wait a little while.

Publishing Packages

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

Copy Code code as follows:

NPM Publish < Local path >

If you want to update the package, just change the version number in Package.json, and then execute the release command again.

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.