#1 2017-08-23 09:07:10

oz
Member
Registered: 2015-09-02
Posts: 98

How to integrate node.js NPM modules to SyNode?

Hi all,
I like to integrate some node.js NPM packages into one of my projects using SyNode/Firemonkey engine. Now I'm searching for some informations on how to achieve that.
My current goal is to integrate the "jsreport-core" (https://www.npmjs.com/package/jsreport-core) package with all of its dependencies and use that in one of my SOA Services.
This is a minimalistic JavaScript example function which I'm trying to execute in SyNode:

var jsreport = require('jsreport-core')()
 
jsreport.init().then(function () {     
   return jsreport.render({
       template: {
           content: '<h1>Hello {{:foo}}</h1>',
           engine: 'jsrender',
           recipe: 'phantom-pdf'
        },
        data: {
            foo: "world"
        }
    }).then(function(resp) {
     //prints pdf with headline Hello world 
     console.log(resp.content.toString())
   });
}).catch(function(e) {
  console.log(e)
})

It fails on the first "require" call not being able to find "jsreport-core" package.
For a quick test I copied "jsreport-core" folder from my local NPM installation to my application bin dir and changed

var jsreport = require('jsreport-core')()

to:

var jsreport = require('./jsreport-core')()

Now evaluation fails with message "cannot find module bluebird" which is one of "jsreport-core" required packages.
My questions are:
- Is it right to copy all those required NPM packages to application's bin directory?
- What needs to be done to "register" all required packages in SyNode so that "require('jsreport-core')" function calls do work?
- Are there any other tasks required to be done for integrating such NPM packages to SyNode?
- Is there some kind of faq/documentation about how to integrate NPM packages to SyNode?

King regards,
oz.

Last edited by oz (2017-08-23 09:08:15)

Offline

#2 2017-08-29 13:05:51

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: How to integrate node.js NPM modules to SyNode?

require in SyNode work in the same way as in nodeJS, so you can

npm install

in the folder with your executable - this will place a module and all dependencies into the node_modules folder.
But you can't use a jsreport-core - this module is asynchronous (use Promises), but SyNode is synchronous.

Offline

Board footer

Powered by FluxBB