现在的位置: 首页 > 综合 > 正文

How to create new module in npm

2013年08月04日 ⁄ 综合 ⁄ 共 1810字 ⁄ 字号 评论关闭

base on the webset http://www.hacksparrow.com/create-npm-package-node-js-module.html

1Add two configure file

You should add two configure file in you project, they are package.json and README.md files.

package.json like this:

{
    "name": "myweb",
    "version": "0.0.1",
    "description": "Node.js module to do some thing",
    "preferGlobal": "true",
    "main": "index.js",
    "bin": { "myweb": "index.js" },
    "author": "Danhuang",
    "keywords": ["myweb", "nodejs", "nodejs framework"],
    "repository" : {
        "type": "git",
        "url": "https://tnodejs@github.com/tnodejs/myweb-nodejs.git"
    },
    "dependencies": {
        "commander": "0.5.2"
    },
    "engines": { "node": "*" }
}

Since we'd want myweb to be used as a commandline tool, we have set "preferGlobal": "true" and "bin": { "myweb": "index.js" }. If it were a library module, that would not have been required.

You must check that the name is your project name, and url in the repository that we can access to the project code in github. 

README.md is the 'introduction and guide file' for the project, formatted in GitHub flavored Markdown(http://github.github.com/github-flavored-markdown/).

2You can add module in linux, windows, mac. 

A. cd myweb directory and run npm link

npm link

B. If it throw the permission denie, then you cab run the sudo npm link

sudo npm link

C. After that we will find a new floder which name is 'node_modules' in myweb directory.

D.  But we don't want its contents to be part of the git repository, so let's add node_modules to.gitignore

echo node_modules/ >> .gitignore

E. Now we should add autor name and password.

Then, you will input your name, password and email.

npm adduser

F. After all of the step, we can publish the nodejs module.

npm publish

3Test for the npm module

We can use npm install the module that we had added before.

 npm install myweb

After runing the it, we will find our module(myweb) stay in the node_modules floder.

抱歉!评论已关闭.