Can not find jsreport-proxy module
-
Hi
I have tried to use jsreport-proxy for merging my pages, but I got error 'Can't find module'
For creating pdf I use node.js and jsreport-core with packages:
jsreport-pdf-utils, jsreport-handlebars, jsreport-chrome-pdf, jsreport-scripts
Do I need some additional packages for possibility to use module jsreport-proxy?
Thanks for help)
-
hi! can you show some code? maybe there is something wrong in the way that you are using, best if we can take a look
-
Hi, sure:
private async initializeInstance(): Promise<any> {
try {
/eslint-disable /
const instance = require("jsreport-core")({
httpPort: 3000,
reportTimeout: 180000,
extensions: {
"xlsx": xlsxExtension,
"chrome-pdf": chromePdfExtension,
"html-to-text": htmlToTextExtension,
express: {
inputRequestLimit: "1000mb"
},
assets: {
allowedFiles: "/.",
searchOnDiskIfNotFoundInStore: false,
publicAccessEnabled: true
}
},
templatingEngines: {
numberOfWorkers: 4,
strategy: "http-server"
},
logger: {
console: {
transport: "console",
level: "error"
}
}
});
instance.use(require('jsreport-pdf-utils')());
instance.use(require('jsreport-handlebars')());
instance.use(require('jsreport-chrome-pdf')());
instance.use(require('jsreport-proxy')({})); <-- HERE IS AN ERROR: Cannot find module 'jsreport-proxy'
await instance.init();
this.instance = instance;
this.initializedInstance = true;
} catch (err) {
throw new Error(err);
}
if (this.initializedInstance) {
this.instance.afterRenderListeners.add("listener", (req: any, res: any) => this.afterRender.call(this, req, res));
this.instance.renderErrorListeners.add("listener", this.renderError);
}
}
-
instance.use(require('jsreport-proxy')({})); <-- HERE IS AN ERROR: Cannot find module 'jsreport-proxy'
The jsreport-proxy is a "virtual" module you can require only from inside a custom script.
Please refer to the documentation here
https://jsreport.net/learn/scripts
-
Sorry, but I still can't understand: should I require jsreport-proxy only inside initialized jsreport instance?
-
you can only require('jsreport-proxy') inside the script file, it is a "virtual" module because it only exists inside the script execution, it is not something published to npm so you can not access it outside of a script, you only need to have
instance.use(require('jsreport-scripts')());
if want to have the jsreport-proxy suppor tin the scripts
-
Sorry for probably such obvious questions =)
Now I'm tried do like this:instance.use(require('jsreport-pdf-utils')());
instance.use(require('jsreport-handlebars')());
instance.use(require('jsreport-chrome-pdf')());
instance.use(require('jsreport-scripts')());
await instance.init();
if (this.initializedInstance) {
this.instance.beforeRenderListeners.add("listener", (req: any, res: any) => this.beforeRender.call(this, req, res));
this.instance.renderErrorListeners.add("listener", this.renderError);
}
private beforeRender(req: any, res: any) {
console.log('\n\n\n beforeRender LOG \n\n\n');
const jsreport = require('jsreport-proxy'); <-- here an error
}And I still get error "Cannot find module 'jsreport-proxy'"
Probably I don't understand how to use jsreport-scripts ?
-
Hi, this would be minimal example
process.env.DEBUG = 'jsreport' const jsreport = require('jsreport-core')() jsreport.use(require('jsreport-scripts')()) async function test() { await jsreport.init() const res = await jsreport.render({ template: { content: 'main', engine: 'none', recipe: 'html', scripts: [{ content: ` const jsreport = require('jsreport-proxy') async function beforeRender(req, res) { const renderRes = await jsreport.render({ template: { content: 'another template', engine: 'none', recipe: 'html' } }) req.template.content = renderRes.content.toString() } ` }] } }) console.log(res.content.toString()) } test().catch(console.error)
I apologize, our main focus in the docs is on running full jsreport with studio as an external reporting server. So the use cases like yours aren't covered, because it isn't our primary goal. Its on our radar to invest time into this and document it, but we are still too busy with other tasks. Hopefully the typescript typings we are currently working on will help also.
I would recommend always try to normally install jsreport with studio, even you won't be using it later.
Try to prepare your reports there and understand the principles first.
Then switch to nodejs and try to reach the same request without studio.
Every extension has its API section like here the scripts that mentions how call without a stored entity. You typically need to call the same just from core.
-
Hi.
I suppose, I got a main point)
Many thanks for help =)