Install and use npm modules from within reports during realtime
This extension allows you to install and use custom npm modules without a need to access the environment where is jsreport hosted. This can be very handy during common reports development because it saves time for server access and restarts. Also it opens the new possibilities in locked environments like jsreport online or playground.
The extension exposes async jsreport.npm.require
function which installs and loads the requested module. The module needs to be specified in form [moduleName]@[moduleVersion]
.
const jsreport = require('jsreport-proxy')
const moment = await jsreport.npm.require('moment@2.29.1')
function myHelper() {
return "Today is " + moment()
}
Note the jsreport-proxy
is a virtual module automatically provided to your sandbox, you can't install it from the package manager.
The extension exposes custom helper npmModule
that reads the bundled distribution of the module. The module needs to be specified in the form [moduleName]@[moduleVersion]
.
handlebars
<script>{{npmModule "moment@2.29.1"}}</script>
<script>console.log(moment())</script>
<script>{{:~npmModule("moment@2.29.1")}}</script>
<script>console.log(moment())</script>
The usage of the npm modules is opt-in and can be enabled in the following ways:
trustUserCode:true
for the whole serverextensions.npm.allowedModules:"*"
extensions.npm.allowedModules:["moment@2.29.1"]
and explicitly list modules users can require