There is a guide https://nginx.org/en/docs/njs/node_modules.html guide that describes how to use 'native' nodejs modules with njs.
I followed the guide until I did not understand what it means in bold:
Note that in this example generated code is not wrapped into function
and we do not need to call it explicitly. The result is in the "dist" directory:$ cat dist/wp_out.js code.js > njs_dns_bundle.js
Let's call our code at the end of a file: <<<--- HERE
var b = set_buffer(global.dns);
console.log(b);And execute it using node:
$ node ./njs_dns_bundle_final.js
Question is how do I include / require / import the webpack generated njs_dns_bundle.js in njs_dns_bundle_final.js which is the name of the Let's call our code at the end of a file since without it I get the error:
njs_dns_bundle_final.js:1
var b = set_buffer(global.dns);
ReferenceError: set_buffer is not defined
My code.js:
module.exports = {
hello: function set_buffer(dnsPacket) {
// create DNS packet bytes
var buf = dnsPacket.encode({
type: 'query',
id: 1,
flags: dnsPacket.RECURSION_DESIRED,
questions: [{
type: 'A',
name: 'google.com'
}]
})
return buf;
}
}
My njs_dns_bundle_final.js:
var myModule = require('./njs_dns_bundle');
var b = myModule.hello(global.dns);
console.log(b);
Node runs fine I think?!:
node ./njs_dns_bundle_final.js
<Buffer 00 01 01 00 00 01 00 00 00 00 00 00 06 67 6f 6f 67 6c 65 03 63 6f 6d 00 00 01 00 01>
NJS does not:
njs ./njs_dns_bundle_final.js
Thrown:
Error: Cannot find module "./njs_dns_bundle"
at require (native)
at main (njs_dns_bundle_final.js:1)
Thanks