nginx/njs

NJS failed to import ParcelJS bundled file

Closed this issue · 1 comments

Hi guys.
I am facing some issue importing external libraries for njs use in nginx.conf as a script.
I have no issue when doing import/require for func1function the being bundledfrom bundle.js locally. Therefore I assume the bundle.js is valid
However, when I tried the to run the nginx.conf below, I have the can't find module issue. which is the reason why I am here to seek for help as I couldn't find any much information about it.

FILES

bundle.js - bundled result that process by parcelJS. function exported: func1()
test.js - js file imported by nginx and execute functions needed function exported: testing()

nginx.conf

load_module modules/ngx_http_js_module.so;
events {  }
http {
    js_path "/etc/nginx/";
    js_import main from test.js;
    server {
        listen 80;
        location /trial {
            js_content main.testing;
        }
    }
}

test.js

const main = require('/etc/nginx/bundle.js')
async function testing(r) {
	// r.error - stamp out the string but not affecting the process
	r.error('REACH HERE')
	r.return(200, `${main.func1()}`)
}
export default {testing}

Issue

[error] exception: Error: Cannot find module "/etc/nginx/bundle.js"
	at require (native)
	at module (/etc/nginx/test.js:1)

Hi @evanNyew,

require() is outdated and is not fully supported, instead use import 'bundle.js' or import main from '/etc/nginx/bundle.js'. require() can only load builtin modules, whereas import syntax allows to load external files.