欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

How to create new module in npm

程序员文章站 2022-03-18 19:45:29
...

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://[email protected]/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.