Skip to content

Node Modules

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

  1. 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
1. 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);
});
1. Be aware of the module size. The free tier of Backendless limits the deployment size to 5 mb. it can be expanded to 20mb with a function pack available in Backendless Marketplace. 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);
});