👩💻Using external NPM packages
How to add external NPM packages to a code node.
Last updated
How to add external NPM packages to a code node.
Last updated
Keep in mind, that most use cases can be handled with a structure node. Please try to avoid creating new code nodes if possible.
Start out with a blank project: only the "start" and "log" nodes should be on the canvas.
Right click on the canvas, then select “Add node”.
In the bottom left corner of the pop up window, click “New Code Node”.
Click on the new node to bing up the inspector panel (right sidebar). Make sure you click on the body of the node, and not one of the ports.
On the inspector panel, select the “Code” tab.
Type the names of NPM packages into the "Dependencies" field, one package name per line. When the project starts, (or any project that uses this node) CRANQ will install the latest version of these packages. Specifying package versions for dependencies is not yet available.
We'll go though the following example: our new code node will receive a date in the format "YYYYMMDD", and return number of years between that date and today.
First, add an input and an output port to the node. (Click the tiny "+" button on each side of the node.)
Click to the "in" port.
In the inspector panel, select the "Code" tab add copy the following code into the editor:
Add a "data/Store" node via the repo, (green plus button) and set a date on its "data" port in the "YYYYMMDD" format.
Connect the "read" input of the "store" node to the "start" node.
Connect the "data" output of the "store" node to the "in" input our code node.
Connect the "out" output of our code node to the "data" input of the "log" node.
Click run
Compile your project. (Menu / Compile / NodeJs)
Uncompress the compiled "tgz" file, and look in the unzipped folder.
You'll find all NPM dependencies in the package.json file.