NodeJS module development and release details _ javascript skills

Source: Internet
Author: User
Tags install node
NodeJS is a young language, and its extension modules are not very comprehensive. We often want to use a module but cannot find a suitable one. For example, I need to use hmac and sha1 for signature two days ago, we didn't find a useful module. At this time, we need to implement the corresponding functions by ourselves. After writing it, You can package it into a module and share it with everyone. This makes it easy for others to feel a little sense of accomplishment. It is a great thing to do. Next, I will introduce how to encapsulate a NodeJS module and share it with others.

NPM (Node Package Manager, is Node. js module management software, in addition to NodeJS built-in core modules, other modules of the installation, uninstall and other management operations must be carried out through NPM, the module we write is released to NPM for use by others.

Next, we will make a very simple module "hello", which has only one function: provide a parameter "name", which outputs "Hello name" on the console ". Before we start, we should first install node and npm. The installation method is introduced on its official website, so we will not talk about it here.

First, create a directory named "hello" as the main directory of the module. Go to this directory and start our work.

Then, write the core code of the module, which is very simple. There are only three lines:

The Code is as follows:

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

Save it as hello. js.

Each node. js extension module has a package. json file that describes some basic attributes of the module, such as the module name, author, and version number. For more information about how to write package. json, run the "npm help json" command.

Run npm init in the main directory of the module to generate the most basic package. json. Follow the command prompts to enter information in sequence. The following is the result of executing npm init in the hello directory and filling in the relevant information:

The Code is as follows:

$ Npm init
Package name: (hello) // Module name. npm init automatically takes the current directory name as the default name. You do not need to change it here. Just confirm it.
Description: A example for write a module // module Description
Package version: (0.0.0) 0.0.1 // The module version number, which can be customized according to your habits.
Project homepage: (none) // the main page of the module. If yes, you can enter it here or not.
Project git repository: (none) // git repository of the module, optional. Npm users generally use github as their git repository.
Author name: Elmer Zhang // module Author name
Author email: (none) // module Author email
Author url: (none) // module Author URL
Main module/entry point: (none) hello. js // module entry file. Here is hello. js.
Test command: (none) // Test script, optional
What versions of node does it run on? (~ V0.5.7) * // The dependent node version number. We can run this script on any node version. Therefore, enter *
About to write to/home/elmer/hello/package. json
// Preview the contents of the generated package. json File
"Author": "Elmer Zhang (Http:// )",
"Name": "hello ",
"Description": "A example for write a module ",
"Version": "0.0.1 ",
"Repository ":{
"Url ":""
"Main": "hello. js? 1.1.9 ",
"Engines ":{
"Node ":"*"
"Dependencies ":{},
"DevDependencies ":{}
Is this OK? (Yes) // After confirming that the above content is correct, you can press enter to confirm it.

So far, we have finished writing this module. At this time, the hello Directory should have two files: hello. js and package. json.

We can return to the "hello" parent directory to test and install this module:

The Code is as follows:

$ Npm install hello/

The installation is successful. A simple test:

The Code is as follows:

$ Node
> Var Hello = require ('hello'). hello;
> Hello ('World ');

Hello world correctly outputs "Hello world ".

Next we will release it to NPM.

First, we need an NPM account. You can use npm adduser to register one:

The Code is as follows:

$ Npm adduser
Username: elmerzhang

In three simple steps, an NPM user is successfully registered.

Finally, return to the hello root directory and run npm publish. If no error message is displayed, the release is successful. Go to click it. Your website should be displayed in the latest Updates column.

Now, a NodeJS module has been successfully released to NPM, And you can install your module through npm install wherever you can access the npm library.
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: 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.