Node.js® JavaScript Module Example Implementation

Setting up a JavaScript Module

To create a new JavaScript module, proceed as follows (assuming that Node.js® is installed).

  1. Decide on a name for the Node.js® module you want to create (called <modulename> in the following.)
  2. Copy this directory into a project in directory javascript/<modulename>
    Note: In case the copied files are read-only, it is necessary to enable write access using OS tools
  3. In javascript/<modulname>/package.json, enter modulname as the value for "name", e.g.:
    {
     "name": "<modulename>",
     ...
    }
  4. Open a command prompt in the copied javascript/<modulename> directory.
  5. Call the following command to install required modules
    npm install
  6. Add a JavaScript Manager with the path of index.js in the copied directory as its only parameter (relative to directory javascript, e.g.: <modulename>/index.js). Set Start mode to manual
    Figure 1. Add a JavaScript Manager
  7. Start the manager - it will terminate immediately
  8. Check that the message "JavaScript Manager for WinCC OA working" is shown in the Log Viewer.
  9. Start developing. Below you can find the JavaScript module example:
    'use strict';
    // The command line for the Node.js® manager must contain the path to
    // this file relative to data/nodejs, e. g.:
    //
    // modulename/index.js
    
    // require WinCC OA interface
    const { WinccoaManager } = require('winccoa-manager');
    const winccoa = new WinccoaManager();
    
    // main function
    async function main() {
      const dpeName = 'ExampleDP_Arg1.';
      const value = await winccoa.dpGet(dpeName);
    
      console.info('Node.js® manager for WinCC OA working');
      console.info('Value of ' + dpeName + ' = ' + value);
    
      winccoa.exit(0);
    }
    
    // start the main function
    main();
    Note: JavaScript code must be called from inside a method or function to prevent unexpected or undefined behavior.
  10. (optional, but recommended) Check and format your code with:
    npm run lint
    npm run format
Tip: Although it is not necessary, nit might be helpful to tell the IDE the location of the TypeScript type definitions to allow auto-completion and displaying of method details. This can be done with IDE settings or by the following command:
npm install --save-dev
Either way, the type definitions are located in <WinCC OA installation path>/javascript/@types/winccoa-manager.