Node.js® TypeScript Module Example Implementation
Setting up a TypeScript Module
To create a new TypeScript module, proceed as follows (assuming that Node.js® is installed).
- Decide on a name for the Node.js® module you want to
create (called
<modulename>
in the following.) - 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
- In javascript/<modulname>/package.json, replace the
value for
"name"
with the name of the module, which should be the same as the directory name by convention, e.g.:{ "name": "<modulename>", ... }
- Open a command prompt in the copied javascript/<modulename> directory.
- Call the following command to install required modules
including a path to the winccoa-manager type definitions (which are needed by
the TypeScript compiler).
npm install --save-dev <path-to-installation>/javascript/@types/winccoa-manager
Note: Currently, the TypeScript plug-in for EsLint requires an EsLint version that uses deprecated packages, so expect to see some warnings when installing these packages. - Transpile TypeScript files to JavaScript by running the following command from the
copied directory:
Or start a watcher, which will automatically transpile TypeScript files when they are changed:npx tsc
npx tsc -w
It will generate index.js in this directory, which will be used by JavaScript manager in the next step.
- Add a JavaScript Manager to the console with the path of index.js in the copied directory as its only parameter (relative to javascript, e.g.: <modulename>/index.js). Set Start mode to manual.
- Start the manager - it will terminate immediately.
- Check that the message "
JavaScript Manager for WinCC OA working
" is shown in the Log Viewer. - Start developing. Below you can find the TypeScript
module example:
// The command line for the Node.js® manager must contain the path to // the transpiled file for this file relative to data/nodejs, e. g.: // // modulename/index.js // // NB: since the transpiled file must be used by the manager, the // extension is .js, not .ts // import WinCC OA interface import { WinccoaManager } from '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: TypeScript code must be called from inside a method or function to prevent unexpected or undefined behavior. - (optional, but recommended) Check and format your code with:
npm run lint npm run format