原文鏈接:http://blog.csdn.net/zeping891103/article/details/50786259
原諒原版鏈接:https://github.com/nwjs/nw.js/wiki/Using-Node-modules
在Node.js中有三種模塊類型:
1)內部模塊(部分Node API)
2)用JavaScript寫的第三方模塊
3)C/C++插件的第三方模塊
這些所有的模塊類型,都在會在node-webkit中使用到。你可以在Node's wiki中找到很多這方面的資料和開源代碼。
如
https://github.com/nodejs/node-v0.x-archive/wiki/Modules
https://www.npmjs.com/
(一)Internal modules(內部模塊)
內部模塊的Node.js同樣可以在node-webkit中直接使用,詳細請查看Node.js的API:
https://nodejs.org/docs/latest/api/
例如,你可以使用var fs = require('fs')直接啟動Node.js的File System的API:
https://nodejs.org/docs/latest/api/fs.html
例如,你可以直接使用 the process 模塊(沒有任何require(....))。然而,建議使用Node.js的API盡量使用require(....)語句來調用,如the process 模塊使用為require(process)。
(注):當前,Node.js API 和在node-webkit的Node.js還是有些區別的,可以參考:
https://github.com/nwjs/nw.js/wiki/Changes-related-to-node
(注):Node.js API參考如下:
https://nodejs.org/docs/latest/api/
(二)3rd party JavaScript modules(用JavaScript寫的第三方模塊)
如果第三方模塊用純JavaScript寫,即不包含任何C/C++插件代碼,那么這個模塊也node-webkit中同樣也可以使用Node內部模塊(require(...))。但這里需要重點注意一個問題:
想要使用JavaScript編寫的第三方模塊,你的應用的根目錄必須有一個命名為node_modules的文件夾,該文件夾為node-webkit默認使用JavaScript寫的第三方模塊使用目錄。假設有個第三方JavaScript模塊名為a_modules,有兩種調用方法:
1)如果使用require(a_modules)的方法調用,則無需添加任何導入語句。
2)如果使用像jQuery的方法調用,如a_modules.(...),則需要添加導入語句<script src="..."> 。
下面我們主要介紹第一種調用情況,因為該調用方法可以很好地隱藏了調用的相對地址,而且會更加便捷。
(1)將已經嵌入到node-webkit的內部模塊代碼獲取至源碼根目錄的node_modules文件夾
這種方法可以讓開發者閱讀到內部模塊的源碼及對其進行擴展。下面以內部模塊之一的async為例。正常情況下,我們在無需添加導入語句,即可使用async,只需調用如下語句:
- var async = require('async');
下面我們將介紹如何獲取async的類庫源碼,以下為Windows系統環境為例:
只需調用命令行即可
- cd /path/to/your/app
- npm install async
這樣你就可以獲取該類庫源碼,源碼位置在你的項目根目錄node_modules的文件夾
- .
- ./package.json
- ./index.html
- ./node_modules
- ./node_modules/async
- ./node_modules/async/.gitmodules
- ./node_modules/async/package.json
- ./node_modules/async/Makefile
- ./node_modules/async/LICENSE
- ./node_modules/async/README.md
- ./node_modules/async/.npmignore
- ./node_modules/async/lib
- ./node_modules/async/lib/async.js
- ./node_modules/async/index.js
這時候你就可以查閱並擴展async模塊。
(注):博主不建議隨意擴展官方已提供的內部模塊,但可以擴充內部模塊。
(2)使用第三方或自己編寫的類庫,擴充內部模塊。
假設你有一個類庫yy庫,你想在你的應用中可以使用require(yy)的方法進行調用,內部擴充了一個yy庫,該如何做呢?
1)在你的項目根目錄下新建文件夾node_modules,在該文件夾中新建yy文件夾,作為你調用的yy庫的地址。
dist目錄和lib目錄下的yy.js就是你要編寫的yy庫的源碼文件,而yy庫下package.json文件則是yy庫的配置文件。
2)yy.js編寫的代碼格式如下:
- (function() {
- var yy = {};
- yy.hello = function() {
- return "hello";
- };
- // Establish the root object, `window` (`self`) in the browser, `global`
- // on the server, or `this` in some virtual machines. We use `self`
- // instead of `window` for `WebWorker` support.
- var root = typeof self === 'object' && self.self === self && self ||
- typeof global === 'object' && global.global === global && global ||
- this;
- // Node.js
- if (typeof module === 'object' && module.exports) {
- module.exports = yy;
- }
- // AMD / RequireJS
- else if (typeof define === 'function' && define.amd) {
- define([], function() {
- return yy;
- });
- }
- // included directly via <script> tag
- else {
- root.yy = yy;
- }
- }());
3)yy庫下package.json文件內容如下:
- {
- "name": "yy",
- "description": "yy lib",
- "main": "lib/yy.js",
- "files": [
- "lib",
- "dist/yy.js"
- ]
- }
這樣你就可以在你的應用中使用yy庫
(三)3rd party modules with C/C++ addons(C/C++插件的第三方模塊)
這塊內容較為復雜,對於一般的開發者也比較少用,同時由於博主對C/C++不是很熟悉,待有空時重新撿起C/C++,再做補充。如需要了解的開發者仍可以閱讀如下地址:
https://github.com/nwjs/nw.js/wiki/Using-Node-modules