Using Node.js Module

Supported Node.js Version

webOS TV supports the different version of Node.js core modules depending on the webOS version as below.

webOS Version Node.js Version
webOS 1.x v0.10.15
webOS 2.x v0.10.25
webOS 3.x v0.12.2
webOS 4.x v0.12.2

Using Node.js Core Modules

To use Node.js core module, add the module with a top-level identifier to your service as the following code.

var Service = require(‘webos-service’);
var fs = require(‘fs’);

var service = new Service("com.palm.service.helloworld");

service.register(“writeFile", function(message) {  
  fs.writeFileSync('/media/internal/foobar.txt', 'sample text', "utf8");
    reply: “write complete"

service.register(“readFile", function(message) {  
  fs.readFileSync('/media/internal/foobar.txt', "utf8");
    reply: “read complete"

See File I/O Service to refer to sample code using Node.js core module.

Using Third-party Modules 

There are many third-party modules for Node.js. To use the third-party modules, follow the next steps.

  1. Use npm (Node Package Manager) tool in your computer to install the third-party module. The module will be installed in the node_modules directory under node.js installation folder. 
    See Node Packaged Modules (NPM) for search and installation of these modules.

  2. Create a node_modules directory in your service's root directory. 

  3. Copy modules from the node_modules directory under the node.js folder to node_modules under your service's root directory.

Now, add the third-party module to your service as the following code. The node will find the module from the node_modules directory first.  

var md = require('md5');
You cannot use modules including C/C++ addons. You must use modules implemented with JavaScript only.