Node學習筆記(二)

1.package.json詳解Node.js 在調用某個包時,會首先檢查包中 package.json 文件的 main 字段,將其做爲包的接口模塊,若是 package.json 或 main 字段不存在,會嘗試尋找 index.js 或 index.node 做爲包的接口。html

package.json 是 CommonJS 規定的用來描述包的文件,徹底符合規範的 package.json 文件應該含有如下字段。node

name:包的名稱,必須是惟一的,由小寫英文字母、數字和下劃線組成,不能包含空格。git

description:包的簡要說明。github

version:符合語義化版本識別①規範的版本字符串。web

keywords:關鍵字數組,一般用於搜索。npm

maintainers:維護者數組,每一個元素要包含 name、email(可選)、web(可選)字段。json

contributors:貢獻者數組,格式與maintainers相同。包的做者應該是貢獻者數組的第一個元素。數組

bugs:提交bug的地址,能夠是網址或者電子郵件地址。瀏覽器

licenses:許可證數組,每一個元素要包含 type (許可證的名稱)和 url (連接到許可證文本的地址)字段。函數

repositories:倉庫託管地址數組,每一個元素要包含 type (倉庫的類型,如 git )、url (倉庫的地址)和 path (相對於倉庫的路徑,可選)字段。

dependencies:包的依賴,一個關聯數組,由包名稱和版本號組成。

 

實例:

{
"name": "mypackage",
"description": "Sample package for CommonJS. This package demonstrates the required
elements of a CommonJS package.",
"version": "0.7.0",
"keywords": [
"package",
"example"

],
"maintainers": [
{
"name": "Bill Smith",
"email": "bills@example.com",
}
],
"contributors": [
{
"name": "BYVoid",
"web": "http://www.byvoid.com/"
}
],

"bugs": {
"mail": "dev@example.com",
"web": "http://www.example.com/bugs"
},
"licenses": [
{
"type": "GPLv2",
"url": "http://www.example.org/licenses/gpl.html"
}
],
"repositories": [
{

"type": "git",
"url": "http://github.com/BYVoid/mypackage.git"
}
],

"dependencies": {
"webkit": "1.2",
"ssl": {
"gnutls": ["1.0", "2.0"],
"openssl": "0.9.8"
}
}
}

2.本地模式與全局模式相比的特色

模 式                    可經過 require 使用                     註冊PATH
本地模式                         是                                           否
全局模式                         否                                           是

 

3. 如何在npm上發佈本身的包在發佈以前,首先須要讓咱們的包符合 npm 的規範,npm 有一套以 CommonJS 爲基礎包規範,但與 CommonJS 並不徹底一致,其主要差異在於必填字段的不一樣。經過使用 npm init 能夠根據交互式問答產生一個符合標準的 package.json,例如建立一個名爲 byvoidmodule 的目錄,而後在這個目錄中運行npm init:

這樣就在 byvoidmodule 目錄中生成一個符合 npm 規範的 package.json 文件。建立一個index.js 做爲包的接口,一個簡單的包就製做完成了。在發佈前,咱們還須要得到一個帳號用於從此維護本身的包,使用 npm adduser 根據提示輸入用戶名、密碼、郵箱,等待帳號建立完成。完成後可使用 npm whoami 測驗是否已經取得了帳號。

接下來,在 package.json 所在目錄下運行 npm publish,稍等片刻就能夠完成發佈了。打開瀏覽器,訪問 http://search.npmjs.org/ 就能夠找到本身剛剛發佈的包了。如今咱們能夠在

世界的任意一臺計算機上使用 npm install byvoidmodule 命令來安裝它。若是你的包未來有更新,只須要在 package.json 文件中修改 version 字段,而後從新使用 npm publish 命令就好了。若是你對已發佈的包不滿意(好比咱們發佈的這個毫無心義的包),可使用 npm unpublish 命令來取消發佈。

4.調試

一.Node.js 支持命令行下的單步調試。

二.使用 Eclipse 調試 Node.js 

三.使用 node-inspector 調試 Node.js 

在命令行下執行 node debug xx.js,將會啓動調試工具:

                        命 令                                                          功 能

                         run                                         執行腳本,在第一行暫停

                      restart                                                 從新執行腳本
                      cont, c                                   繼續執行,直到遇到下一個斷點
                      next, n                                                        單步執行
                      step, s                                               單步執行並進入函數
                       out, o                                                    從函數中步出
             setBreakpoint(), sb()                                   在當前行設置斷點
          setBreakpoint(‘f()’), sb(...)                         在函數f的第一行設置斷點

      setBreakpoint(‘script.js’, 20), sb(...)              在 script.js 的第20行設置斷點
            clearBreakpoint, cb(...)                                  清除全部斷點
                   backtrace, bt                                         顯示當前的調用棧
                      list(5)                                         顯示當前執行到的先後5行代碼
                 watch(expr)                                       把表達式 expr 加入監視列表
              unwatch(expr)                                     把表達式 expr 從監視列表移除

                watchers                                        顯示監視列表中全部的表達式和值

                   repl                                                在當前上下文打開即時求值環境
                    kill                                                       終止當前執行的腳本
                scripts                                                 顯示當前已加載的全部腳本
                version                                                      顯示 V8 的版本

下面是一個簡單的例子:
$ node debug debug.js
< debugger listening on port 5858
connecting... ok
break in /home/byvoid/debug.js:1
1 var a = 1;
2 var b = 'world';
3 var c = function (x) {
debug> n

break in /home/byvoid/debug.js:2
1 var a = 1;
2 var b = 'world';
3 var c = function (x) {
4 console.log('hello ' + x + a);
debug> sb('debug.js', 4)
1 var a = 1;
2 var b = 'world';
3 var c = function (x) {

* 4 console.log('hello ' + x + a);
5 };
6 c(b);
7 });
debug> c
break in /home/byvoid/debug.js:4
2 var b = 'world';
3 var c = function (x) {
* 4 console.log('hello ' + x + a);
5 };
6 c(b);

debug> repl Press Ctrl + C to leave debug repl > x 'world' > a + 1 2 debug> c < hello world1 program terminated

相關文章
相關標籤/搜索