With the introduction of the Custom Code Codeless block, it is now possible to use functionality from NodeJS modules in Codeless logic. This recipe demonstrates the process of importing an NPM module and using it in a Codeless API Service.
A sceptic reader may point out that using the
Custom CodeinCodelessis an oxymoron. Technically, it is, however, any code you place inside ofCustom Codewould be seen in your overall logic as oneCodelessblock, so I’d continue to maintain the position that it is still Codeless
Back to the recipe… to demonstrate the usage of NodeJS npm module in a Codeless API service, we will use the Fibonacci npm module. The module provides the functionality of calculating the Fibonacci number for the provided number of iterations. The usefulness of the functionality is rather narrow, however, the approach for deploying the code into Backendless and using it in Cloud Code is universally applicable to any other node module.
Prerequisites:
You must have the NPM installed locally. When you run the npm command in a Terminal/Command Prompt window, you should be getting a response that indicates that npm is installed. For example:
Using NPM modules in Backendless Business Logic (Cloud Code) requires installing all the modules locally first.
- Open a command prompt window and create (or navigate to) a directory where you will download all the required modules. In my case, the directory is called
/fib - Run the following command:
Thenpm install --save fibonacci--saveargument is important as it instructs npm to save downloaded modules in the current directory rather than global/shared/main npm storage on your computer. - Once the command above completes, your directory structure should look like as shown below:

- The
node_modulesdirectory contains the actual modules we’re interested in. Open Backendless Console and navigate to theFilessection of your app. Locate theservercodedirectory. Drag and drop thenode_modulesdirectory from your local computer (the directory is created in step #3) intoRoot/servercodedirectory in console. Once the directories and files are uploaded, yourservercodedirectory in console should look like this:

when navigating to thenode_modulesdirectory in Backendless Console, you should see the following:

- Now that the modules have been deployed, switch to the Business Logic section and create a new Codeless API service.
- Click
SAVEand you will be prompted to create a method for your service:
- Ope the method’s logic for editing. Drag the
Custom Codeblock into the method’s logic and connect it with thereturnconnector:
- Click the
Add a new argumentfield and type initand press Enter. This will create an argument for theCustom Codeblock called `it. Paste the following code into the editor:
Notice the first line imports theconst fibonacci = require ('fibonacci'); return fibonacci.iterate (it);fibonaccimodule. The module is imported from thenode_modulesdirectory you deployed to your Backendless app in one of the steps above. - Click the
SAVE AND CLOSEbutton. Connect theMethod Argument iterationsto yourCustom Codeblock:
- Finally click the
DEPLOY MODELbutton to deploy your API Service:
When you get a confirmation saying the service has been deployed, you can switch to the API SERVICES tab and invoke the service. Don’t forget to enter a value for the iterations argument:
Now you have a Codeless API services that use an NPM module with custom code.





