How to publish a custom node. js module to NPM (detailed steps)

Source: Internet
Author: User
Tags using git

We don't talk much, just start!

Since I have never used a Mac, I don't guarantee that the action described in this article is consistent with your Mac.

I begin by assuming that you have already installed node. js in Windows Global, which begins with the detailed steps described below:

This article in the spirit, with very little text description and very little code writing as the principle to show you!

The module uploaded in the article does not have any meaning!

I. Required items when encapsulating a node. js module

1. Create Package.json

Each full-encapsulated node module must contain a Package.json file with a clear parameter!

The following are the most streamlined configurations of Package.json:

{   
"Name": "Npmdesc", "version": "0.0.1",
"Main": "Npmdesc.js"}

Package.json Detailed configuration reference table:

2. Create Npmdesc.js

Console.log ("How to publish a custom node. js module to NPM (detailed steps)")

3. Create the Readme.md file, the contents of the file can be left white (the role of the file is to place any readme instructions you would like to write)

Second, the Generation module

1. In the root directory of your project, open the Console window, type commands

NPM Pack

At this point NPM will package your project as a compressed package with a suffix called. tgz, which is your node. JS Module

Third, build a common repository containing the module code on GitHub

1. Register a GitHub account (not detailed here, assuming you already have a GitHub account)

2. Create a project

3. If you're using Git for the first time, learn how git is used (just a simple description here)

It is recommended to see how Liaoche teaches git: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

1.ssh-keygen-t rsa-c "You register to use the mailbox" 2. Login to GitHub, open "Account Settings", "SSH Keys" page point "Add ssh Key", fill in ssh, paste in the Key text box id_ The contents of the Rsa.pub file.

The above steps are the core steps to get your account to build communication with Git!
This is not enough, and if you're using Git for the first time, this step may make your foggy confusing and even lose the confidence to continue knowing the rest of the way, so I suggest that beginners start with a simple git understanding.

4. Push the project to the public repository you just created (assuming you've done all of the above and have a simple understanding of git usage)

Run the following git command in the root directory of your project:

1.git Init
2.git Add Npmdesc.js
git add Package.json
git add readme.md
git add npmdesc-0.0.1.tgz
3.git remote Add origin [email protected]:bgonline-cn/npmdesc.git
4.git commit-m "How to publish a custom node. js module to NPM" 5.git pull--rebase Origin Master6.git Push-u Origin Master

The role of the above steps (only to do the auxiliary understanding, the content is not perfect, the specific steps need to refer to the 4th step):

1. Initialize the local git library

2. Staging files (I did not submit all of them here, don't forget to submit them when you first submit)

3. Make the local connection to the remote library

4. Submit to the local library,-m for your submission information, this is required! You need to tell git why you're making this submission.

5. Pull the contents of the remote library

6. Push a local project to the remote library

Since I forgot to add the Readme, the 6th step did not succeed. After I re-added the readme, I re-executed the 5th and 6th steps

There are a variety of unpredictable issues that can occur more or less during GIT operations. If you meet at this time, please don't be afraid, careful groping and powerful search engine can help you! That's the way anyone would come.

Of course I don't rule out the omission of my article--

Iv. Release to NPM

1. Create an account on the Https://npmjs.org website (no introduction here)

2. Add the created account to the environment using the following command at the command line:

NPM AddUser

You will be prompted to enter your username and password and email address, where the password will not be displayed in clear text.

A description command appears to execute successfully!

3. Modify the Package.json file to fill in the information in your project in the Git public repository and the keywords you want NPM to search for

{      "name": "Npmdesc",    "version": "0.0.1",    "main": "Npmdesc.js",    "Repository": {        "type": "Git",        "url": "Https://github.com/BGOnline-CN/npmdesc"     },    " Keywords ": [        " Npmdesc "    ]}

4. Do not forget to push the modified Package.json to git oh ~ Here I do not introduce.

5. Release the module to NPM

Execute the following command at the project root:

NPM Publish

If the above error occurs, use the following command to resolve

You will need to re-login after executing this command, and use the command in step 2nd to

If everything is OK, it will appear:

Now that your node. JS module has been published, you can try searching for it on the NPM website and using it in your project.

How to publish a custom node. js module to NPM (detailed steps)

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.