Skip to content

Node Modules

Custom business logic in JavaScript can take advantage of 3rd party node modules. To add a module:

Install a module

Install a module you want to use with --save option. It is important to use the --save option so that the module is installed at the project level (rather than globally). When a module is installed at the project level, it is added to project.json into the non dev 'dependencies' list. As a result, when the code is deployed to Backendless, the module will be included into the deployment scope.

For example, the command below installs the "lodash" module:

npm install lodash --save

Use the module

Once installed, use the module in the code as usual. For example, the lodash module installed in the step above can be used to set the name of the creation item to camelCase using the lodash library:

var _ = require('lodash');

Backendless.ServerCode.Persistence.beforeCreate('*', function(req) {
  req.item.name = _.camelCase(req.item.name);
});

Deployment size awareness

Make sure that the deployment size doesn't exceed your current Backendless tier limits.  To reduce the deployment size, consider minimizing the installation of the  dependencies to only what the code needs. For example, since the code above uses only the camelCase function, it is possible to install only that module as shown below:

npm install lodash.camelcase --save

Then use the module as:

var camelCase = require('lodash.camelcase');

Backendless.ServerCode.Persistence.beforeCreate('*', function(req) {
  req.item.name = camelCase(req.item.name);
});

Keep in mind that you can extend the limit of a Backendless plan your app is on by adding the function pack from the Marketplace.