NPM import tutorial
To effectively manage ClearBlade libraries, you will use the ClearBlade Command Line Tool and the cb-dev-kit tool for local development and transpiling NPM packages. Below is a step-by-step guide to achieve this, using the moment-timezone NPM package as an example. You can change the instructions for other NPMs accordingly.
Prerequisites
ClearBlade Command Line Tool (cb-cli):
Have the latest version installed.
cb-dev-kit:
Have the latest version installed.
Steps to import NPM packages
Initialize ClearBlade workspace:
Choose a folder on your local machine, either blank or a Git/source-control workspace.
Run
cb-cli init
in the chosen folder.Follow the initialization steps, referring to the ClearBlade system where the transpiled code will run.
Create a dummy library:
Use the ClearBlade console to create a dummy library named
MomentTimezone
in the system.Â
The transpiled NPM package will replace this placeholder library.
Pull the library:
Pull the library into your local workspace using:
cb-cli pull -library MomentTimezone
Open the workspace in IDE:
Open the local workspace in your preferred IDE (e.g., Visual Studio Code).
Â
Initialize cb-dev-kit:
In the same local workspace folder, run:
cb-dev-kit init
Â
Install the node module:
Install the
moment-timezone
NPM module with the following command:npm i --save moment-timezone
Â
Create transpiled library:
Run the following command to create the transpiled library:
NOTE: -t means type and can be ts for TypeScript or js for JavaScript.
You may see component name undefined returned to your terminal. Ignore this.
Locate the new folder:
After successful execution, a new folder will appear in the
src
directory.
Modify the JavaScript or TypeScript file:
Look at the NPM documentation to find the main object to import. For moment-timezone it is moment.
Â
In the new JS or TS file within the
src
folder, add the following lines at the top and delete any other lines:Â
Build the library:
Run the build command:
Â
Push to ClearBlade system:
Push the library to the ClearBlade system with:
This will upload the library, making it available in the ClearBlade system.
Create a test code-service in the system to test the library. Make sure to add the library as a dependency.
For example:Â
Â
Â
By following these steps, developers can import and manage NPM packages as ClearBlade libraries.