Note
- webOS OSE releases from 1.8.0 to 2.6.0 support Node.js v8.12.0.
- webOS OSE releases up to 1.7.0 support Node.js v6.11.2.
This page shows you how to use Node.js modules inside your service.
webOS Open Source Edition supports all the Node.js core modules provided by Node.js v12.14.1. For the list of core modules and API details, see Node.js v12.14.1 Documentation.
To use Node.js core module, add the module with the top-level identifier to your service as the following code.
var Service = require('webos-service');
var fs = require('fs');
var service = new Service("com.mycom.service.helloworld");
service.register("writeFile", function(message) {
fs.writeFileSync('/media/internal/foobar.txt', 'sample text', "utf8");
message.respond({
reply: "write complete"
});
});
service.register("readFile", function(message) {
fs.readFileSync('/media/internal/foobar.txt', "utf8");
message.respond({
reply: "read complete"
});
});
There are many third-party modules for Node.js. To use the third-party modules, take the following steps.
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 the node.js installation
directory.
See Node Packaged Modules (NPM) for search and installation of these modules.
Create a node_modules
directory in your service’s root directory.
Copy modules from the node_modules
directory under the node.js
directory 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 node_modules
directory first.
var md = require('md5');
Contents