Variable scope in javascript module

alweiss nginx-forum at
Tue Oct 30 16:58:53 UTC 2018

Here is a sample that works with Java but not with njs :

function my() {
    resp = "Start";
    console.log ('Initial resp is ' + resp);

    function done() {
        resp += "AndContinue";
        console.log('In loop resp is ' + resp)

resp = 'empty'
console.log('End Resp is : ' + resp)

With java :

root at linux3:/etc/nginx# js test.js
Initial resp is Start
In loop resp is StartAndContinue
End Resp is : StartAndContinue

With NJS :

root at linux3:/etc/nginx# njs test.js
ReferenceError: "resp" is not defined in 16

Don't know why it doesn't work the same in both runtime.


Posted at Nginx Forum:,281699,281747#msg-281747

More information about the nginx mailing list