Home > Web Front-end > JS Tutorial > Detailed explanation and sharing of NodeJS module development and release_javascript skills

Detailed explanation and sharing of NodeJS module development and release_javascript skills

WBOY
Release: 2016-05-16 17:55:44
Original
913 people have browsed it

For example, two days ago, I needed to use hmac and sha1 for signature, but I couldn't find a relatively easy-to-use module. At this time, we need to implement the corresponding functions ourselves. After you finish writing it, you can package it into a module and share it with everyone. This will not only facilitate others, but also give you a small sense of accomplishment. It is really a good thing that serves multiple purposes. Next, I will introduce to you how to encapsulate a NodeJS module and share it with others.

NPM (Node Package Manager, http://npmjs.org) is the module management software of NodeJS. In addition to the built-in core modules of NodeJS, the installation, uninstallation and other management operations of other modules must be performed through NPM. , the modules we write ourselves must be published to NPM for others to use.

Next, we make a very simple module "hello". This module has only one function: providing a parameter "name", which outputs "Hello name" on the console. Before we start, we must first install node and npm. The installation method is introduced on its official website, so I won’t go into details here.

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

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

Copy the code The code is as follows :

exports.Hello = function ( name ) {
console.log( "Hello " name );
}

Save it as hello .js.

Each NodeJS extension module has a package.json file, which is used to describe some basic attributes of the module, such as module name, author, version number, etc. For detailed instructions on how to write package.json, you can use the "npm help json" command to view it.

We can execute npm init in the module's main directory to generate a basic package.json. Just enter the information in sequence according to the command prompts. The following is the result after executing npm init in the hello directory and filling in the relevant information:
Copy the code The code is 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 it here, just confirm it directly
Description: A example for write a module //Module description
Package version: (0.0.0) 0.0.1 //Module version number, you can decide this according to your own habits
Project homepage: (none) //Module If there is one, you can fill it in here, or you can leave it blank
Project git repository: (none) //The 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) freeboy6716@gmail.com //Module author email
Author url : (none) http://www.elmerzhang.com //Module author URL
Main module/entry point: (none) hello.js //The entry file of the module, here we are hello.js
Test command: (none) //Test script, optional
What versions of node does it run on? (~v0.5.7) * //The dependent node version number, our script can run on any version of node , so fill in *
About to write to /home/elmer/hello/package.json
// The following is a preview of the content of the generated package.json file
{
"author": "Elmer Zhang (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) //After confirming that the above content is correct, you can press Enter directly to confirm

At this point, our module has been written. At this time, there should be two files in the hello directory: hello.js and package.json.

We can return to the upper-level directory of hello to test the installation of this module:
Copy the code The code is as follows :

$ npm install hello/
hello@0.0.1 ./node_modules/hello

Shows the installation is successful. Simply test it:
Copy the code The code is as follows:

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

Hello world correctly outputs "Hello world".

Next we publish it to NPM.

First of all, we need to have an NPM account. You can use npm adduser to register one:
Copy code Code As follows:

$ npm adduser
Username: elmerzhang
Password:
Email: freeboy6716@gmail.com

Three simple steps, one NPM User registration is successful.

Finally, return to the hello root directory and execute npm publish. If there are no error messages, then the publication is successful. Go to http://search.npmjs.org/ and take a look. Your module should already be displayed in the "Latest Updates" column.

At this point, a NodeJS module has been successfully published to NPM. You can then install your module through npm install anywhere you can access the npm library.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template