-
Notifications
You must be signed in to change notification settings - Fork 2
Using Node modules
node.js 는 3가지 방식의 모듈이 있습니다.
-
내장 모듈 (Node API 부분)
-
JavaScript로 만든 타사 모듈
-
C/C++ 로 만든 네이티브 타사 모듈
이들을 모두 nw.js 에서 사용할 수 있습니다.
참고: node.js wiki에 기재된 “모듈” 이나, npm 검색 으로 찾은 모듈 등으로 다양한 오픈 소스 모듈을 추가할 수 있습니다.
node.js 내장 모듈은 별다른 과정 없이 node.js 방식으로 추가할 수 있습니다. 모듈 일람은 API 문서를 참고하세요.
예를 들면, var fs = require('fs') 으로 파일시스템 모듈을 사용할 수 있습니다.
또다른 예로 require(…) 같은 별도 첨부 없이 바로 process 모듈을 사용할 수 있습니다.
그러나 node.js API와 nw.js API가 약간 차이점이 있습니다. 자세한 사항은 node.js 와 nw.js API 차이점을 참고하세요.
만약 타사 모듈이 네이티브 없이 순수 자바스크립트로만 짜여진 모듈이라면, 바로 nw.js 에서 require('moduleName') 방식으로 첨부하여 사용할 수 있습니다.
그러나 상대 경로는 앱에 있는 부모 경로가 어떤 방식을 쓰냐에 따라 달라집니다.
-
require()를 사용할 경우, 불러오는 자식을 가져올 때 상대 경로는require()를 사용한 파일 기준으로 결정됩니다. -
만약 웹키트 기반 시스템에서 다른 파일 경로를 불러올 경우 (
window.open(),XMLHttpRequest,<script src="...">등 웹에서 쓰이는 외부 경로 사용 기능) 상대 경로는 앱 최상위 경로부터 시작합니다.
The former rule means that any module's submodules are always being required exactly as in Node and work properly.
However, it is generally wise not to use the explicit relative paths (starting with ../ or ./) at all. Instead of it, just calling require('modulename') is enough — if the module has been put in the /node_modules subdirectory of your application. (See the “Loading from node_modules Folders” section of Node API.)
For example, you may install modules from npm packages by running npm install modulename in your application's directory (where your application's manifest resides), because npm would automatically put these modules in the /node_modules subdirectory.
Here is an example of installing the async module:
$ cd /path/to/your/app
$ npm install asyncHere is the resulting list of files in the whole tree:
$ find .
.
./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.jswhere your application's manifest (./package.json) might look like the following:
{
"name": "nw-demo",
"main": "index.html"
}And here's an example of index.html:
<html>
<head>
<title>test</title>
<script>
var async=require('async');
</script>
</head>
<body>
test should be here.
</body>
</html>For modules containing C/C++ addons the situation is slightly different from the above — and more complex — because the ABI (application binary interface) of node-webkit differs from Node's ABI.
When such a module is installed for Node (with npm install command), npm uses its internal version of the node-gyp tool to build the addons (from their source code) for Node.js.
To build such a module for node-webkit, nw-gyp (a special fork of node-gyp) is necessary. You also have to run nw-gyp manually, because npm (being a Node's tool) does not know anything about building for node-webkit and using nw-gyp.
To install nw-gyp, run npm install nw-gyp -g.
Before actually using nw-gyp, please meet its requirements (you'll need a proper Python engine and C/C++ compiler). These requirements are not different from node-gyp's.
To build a module for node-webkit, you may at first obtain it from an npm package, as if for Node.js (npm install modulename), but then rebuild it for node-webkit (nw-gyp rebuild --target=0.5.0).
Alternatively, you may get the module's source code elsewhere (for example, from GitHub) and run nw-gyp rebuild --target=0.5.0 on it.
In both of these alternatives,
-
nw-gyp must be run in the module's directory (where the module's
binding.gypresides), -
the necessary target version of node-webkit (such as
0.5.0innw-gyp rebuild --target=0.5.0) must be given explicitly, because nw-gyp cannot autodetect it, -
you must rebuild a module before you start using it in any newer version of node-webkit, because the ABI is not stable and changes constantly with the versions. For example,
- after node-webkit 0.6.0 is released,
nw-gyp rebuild --target=0.6.0is necessary, - after node-webkit 0.6.1 is released,
nw-gyp rebuild --target=0.6.1is necessary, - and so on.
- after node-webkit 0.6.0 is released,
The difference of ABI also means that the built C/C++ addons (i.e. .node files) of Node and node-webkit are mutually incompatible: you cannot use an addon in Node if it's built for node-webkit, and vice versa.
For example, you cannot use some node test.js (or npm test) to test an addon-containing module in Node if that addon has been built for node-webkit: the test will always fail (either with some cryptic error message or with a crash of the whole engine).
Important note 1: on Windows the engine's executable file must have the name nw.exe for addons to work (i.e. you cannot rename nw.exe if you need any addon-containing modules). They use parts of the engine to sustain their execution.
Important note 2: on Windows it seems to be neccesary to include the full Python's path in the system's PATH variable.
Important note 3: Remember to replace the original .node file with your generated one, otherwise the module won't work (because it still points to the original Node.js module instead of using the one compiled for node-webkit).
For more information on that matter (including further limitations and known issues), see “Build native modules with nw-gyp”.