|
| 1 | +--- |
| 2 | +title: Creating Node Extensions |
| 3 | +--- |
| 4 | + |
| 5 | +This document outlines how to create node extensions for **Phoenix Code**. |
| 6 | + |
| 7 | +## How to create a new Node Extension |
| 8 | + |
| 9 | +To create a new node extension for Phoenix Code, use [this template](https://github.com/phcode-dev/extension-node-template). This template extension works in the browser as well as desktop builds. In browser, it will not use node, and node.js based functionalities are not available. Desktop builds can use node capabilities. |
| 10 | + |
| 11 | +In desktop builds, there is an additional capability to execute node.js code. This is helpful if you want to extend the functionality of Phoenix Code using the vast npm library. |
| 12 | + |
| 13 | +For creating extensions that do not need node, use: [https://github.com/phcode-dev/extension-template](https://github.com/phcode-dev/extension-template) |
| 14 | + |
| 15 | + |
| 16 | +## Setting up node extensions |
| 17 | + |
| 18 | +In package.json, add the following section |
| 19 | + |
| 20 | +``` |
| 21 | +{ |
| 22 | + "nodeConfig": { |
| 23 | + "nodeIsRequired": false, |
| 24 | + "main": "node/index.js", |
| 25 | + "npmInstall": "node/" |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## nodeConfig Object |
| 31 | + |
| 32 | +The nodeConfig object indicates that this is a Node extension. |
| 33 | +### nodeIsRequired Field |
| 34 | + |
| 35 | +Set this field to `true` if the extension relies on Node and won't function without it. |
| 36 | +If set to `false` or omitted, the extension can still be loaded in browser versions of Phoenix code without Node support, but it will use Node in native builds. |
| 37 | +It will be shown in the extension manager dialog in browser builds as well. |
| 38 | + |
| 39 | +### main Field |
| 40 | + |
| 41 | +Specifies the main entry point for the Node.js component of the extension. |
| 42 | +Should point to the main JavaScript file for the Node part of the extension. |
| 43 | +Example: "main": "node/index.js" |
| 44 | + |
| 45 | +### npmInstall Field (Optional) |
| 46 | + |
| 47 | +Specifies the path to run npm install when the user installs the extension from the extension manager. |
| 48 | +It's advisable not to package node_modules inside the extension. Only the package lock file should be distributed. |
| 49 | +Example: "npmInstall": "node/" |
| 50 | + |
| 51 | +## Communicating between node.js and Phoenix Code |
| 52 | + |
| 53 | +Use the [NodeConnector-API](https://docs.phcode.dev/api/API-Reference/NodeConnector) to call functions and send events between your node.js and Phoenix Code extension components. |
| 54 | + |
| 55 | +This is available as a global object global.createNodeConnector. |
| 56 | + |
| 57 | +[EventDispatcher-API](https://docs.phcode.dev/api/API-Reference/utils/EventDispatcher) is also available in the global context as global.EventDispatcher for event trigger/listen within node. |
| 58 | + |
| 59 | + |
| 60 | +## Using this template |
| 61 | +[Click Here](https://github.com/phcode-dev/extension-node-template/blob/main/README.md#using-this-template) for the instructions on how to use this template. |
0 commit comments