`

How to create new module in npm

 
阅读更多

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

1Addtwoconfigurefile

Youshouldaddtwoconfigurefileinyouproject,theyarepackage.jsonandREADME.mdfiles.

package.jsonlikethis:

{
    "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": "*" }
}

Sincewe'dwantmywebtobeusedasacommandlinetool,wehaveset"preferGlobal":"true"and"bin":{"myweb":"index.js"}.Ifitwerealibrarymodule,thatwouldnothavebeenrequired.

Youmustcheckthatthenameisyourprojectname,andurlintherepositorythatwecanaccesstotheprojectcodeingithub.

README.mdisthe'introductionandguidefile'fortheproject,formattedinGitHubflavoredMarkdown(http://github.github.com/github-flavored-markdown/).

2Youcanaddmoduleinlinux,windows,mac.

A.cdmywebdirectoryandrunnpmlink

npm link

B.Ifitthrowthepermissiondenie,thenyoucabrunthesudonpmlink

sudo npm link

C.Afterthatwewillfindanewfloderwhichnameis'node_modules'inmywebdirectory.

D.Butwedon'twantitscontentstobepartofthegitrepository,solet'saddnode_modulesto.gitignore.

echo node_modules/ >> .gitignore

E.Nowweshouldaddautornameandpassword.


Then,youwillinputyourname,passwordandemail.

npm adduser

F.Afterallofthestep,wecanpublishthenodejsmodule.

npm publish


3Testforthenpmmodule

Wecanusenpminstallthemodulethatwehadaddedbefore.

npminstallmyweb

Afterruningtheit,wewillfindourmodule(myweb)stayinthenode_modulesfloder.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics