How to create new module in npm

來源:互聯網
上載者:User
base on the webset http://www.hacksparrow.com/create-npm-package-node-js-module.html1、Add 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/).

2、You 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

3、Test 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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.