Nodejs Module development and release detailed sharing _javascript skills

Source: Internet
Author: User
For example, the first two days I need to use HMAC and SHA1 to do the signature, I did not find a more useful module, this time we need to implement the corresponding function. After writing, and then packaging it into a module to share for everyone to use, that is, to facilitate the other people, but also to make their own a little sense of achievement, it is a good thing. Next, I'll show you how to encapsulate a Nodejs module and share it with other people.

NPM (Node Package Manager, http://npmjs.org) is Nodejs module management software, in addition to Nodejs built-in core modules, other modules, installation, uninstall and other management operations through NPM, we write the module, we will release Come up to NPM for other people to use.

Next, we make a very simple module "Hello", which has only one function: providing a parameter "name", which outputs "Hello name" to the console. Before we start, we have to install node and NPM, and the installation method is introduced on its official website, and there is no more to say.

First, we create a directory called "Hello" as the home directory for the module. Enter the catalogue and start our work.

Then, write the core code of the module, very simple, only the following three lines:
Copy Code code as follows:

Exports. Hello = function (name) {
Console.log ("Hello" + name);
}

Save it as a hello.js.

Nodejs each extension module has a Package.json file that describes some of the basic properties of the module, such as the module name, author, version number, and so on. For a detailed description of the Package.json, you can use the NPM help JSON command to view it.

We can execute NPM init in the module's home directory to generate a basic package.json. Follow the instructions in the command to enter the information in turn. The following is the result of performing NPM init in the Hello directory and filling in the relevant information:
Copy Code code as follows:

$ NPM Init
Package name: (hello)//module name, NPM Init will automatically take the current directory name as the default name, there is no need to change, direct confirmation can be
description:a Example for write A module//Modules description
Package version: (0.0.0) 0.0.1//Module Edition number, this is the custom you can
Project homepage: (none)//module's homepage, if any, can be filled in here, or not filled
Project git repository: (none)//module Git warehouse, optional. NPM's users typically use GitHub as their own git repository
Author name:elmer Zhang//Module author name
Author Email: (none) freeboy6716@gmail.com//Module Author email
Author URL: (none) http://www.elmerzhang.com//module author URL
Main Module/entry point: (None) hello.js//Module entry file, here we are hello.js
Test command: (none)//testing script, optional
What versions of node does it run on? (~v0.5.7) *//Dependent node version number, we can run this script on any version of node, so fill *
About to write To/home/elmer/hello/package.json
The following is a preview of the generated Package.json file content
{
"Author": "Elmer Zhang <freeboy6716@gmail.com> (http://www.elmerzhang.com)",
"Name": "Hello",
"description": "A example for write a module",
"Version": "0.0.1",
"Repository": {
"url": ""
},
"Main": "Hello.js",
"Engines": {
"Node": "*"
},
"Dependencies": {},
"Devdependencies": {}
}
Is this OK? (yes)//To confirm the above content, you can directly return to confirm the

So far, we have finished this module. There should be two files in the Hello directory: Hello.js and Package.json.

We can go back to Hello's parent directory to test the installation of this module:
Copy Code code as follows:

$ NPM Install hello/
hello@0.0.1./node_modules/hello

Displays the installation success. A simple test:
Copy Code code as follows:

$ node
> var hello = require (' Hello '). Hello;
> Hello (' World ');

Hello world has correctly exported "Hello World".

Next we'll post it on NPM.

First, we need to have a NPM account that can use NPM adduser to register a:
Copy Code code as follows:

$ NPM AddUser
Username:elmerzhang
Password:
Email:freeboy6716@gmail.com

Simple three steps, a NPM user registered successfully.

Finally, go back to the Hello root directory, perform the NPM publish, and if there are no errors, then the release is successful. Take a look at the http://search.npmjs.org/, your module should already be displayed in the "Latest Updates" column.

At this point, a Nodejs module is successfully released to NPM, and you can then install your module via NPM install anywhere you can access the NPM library.

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.